blob: 11b6a18be5cf8aeb34ed04662129fef9dea2f412 [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
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.mielczarek0a5fc5d2009-12-23 17:09:27 +000045#include <elf.h>
nealsidb0baafc2009-08-17 23:12:53 +000046#include <errno.h>
47#include <fcntl.h>
ted.mielczarekcfc86282010-10-20 15:51:38 +000048#if !defined(__ANDROID__)
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000049#include <link.h>
ted.mielczarekcfc86282010-10-20 15:51:38 +000050#endif
nealsidb0baafc2009-08-17 23:12:53 +000051
52#include <sys/types.h>
53#include <sys/ptrace.h>
54#include <sys/wait.h>
55
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000056#include <algorithm>
57
nealsidb0baafc2009-08-17 23:12:53 +000058#include "client/linux/minidump_writer/directory_reader.h"
59#include "client/linux/minidump_writer/line_reader.h"
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000060#include "common/linux/file_id.h"
nealsidb0baafc2009-08-17 23:12:53 +000061#include "common/linux/linux_libc_support.h"
thestig@chromium.org0e3b7022010-09-15 22:31:57 +000062#include "third_party/lss/linux_syscall_support.h"
nealsidb0baafc2009-08-17 23:12:53 +000063
nealsid6bc523c2010-05-10 20:35:54 +000064static const char kMappedFileUnsafePrefix[] = "/dev/";
65
nealsidb0baafc2009-08-17 23:12:53 +000066// Suspend a thread by attaching to it.
67static 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.orgf5c8f6f2010-08-14 01:41:39 +000080#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, &regs) == -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
nealsidb0baafc2009-08-17 23:12:53 +0000100 return true;
101}
102
103// Resume a thread by detaching from it.
104static bool ResumeThread(pid_t pid) {
105 return sys_ptrace(PTRACE_DETACH, pid, NULL, NULL) >= 0;
106}
107
nealsid6bc523c2010-05-10 20:35:54 +0000108inline 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
nealsidb0baafc2009-08-17 23:12:53 +0000119namespace google_breakpad {
120
121LinuxDumper::LinuxDumper(int pid)
122 : pid_(pid),
nealsidde545c02010-03-02 00:39:48 +0000123 threads_suspended_(false),
nealsidb0baafc2009-08-17 23:12:53 +0000124 threads_(&allocator_, 8),
125 mappings_(&allocator_) {
126}
127
128bool LinuxDumper::Init() {
129 return EnumerateThreads(&threads_) &&
130 EnumerateMappings(&mappings_);
131}
132
133bool LinuxDumper::ThreadsSuspend() {
nealsidde545c02010-03-02 00:39:48 +0000134 if (threads_suspended_)
nealsidb0baafc2009-08-17 23:12:53 +0000135 return true;
thestig@chromium.orgf5c8f6f2010-08-14 01:41:39 +0000136 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 }
nealsidde545c02010-03-02 00:39:48 +0000147 threads_suspended_ = true;
thestig@chromium.orgf5c8f6f2010-08-14 01:41:39 +0000148 return threads_.size() > 0;
nealsidb0baafc2009-08-17 23:12:53 +0000149}
150
151bool LinuxDumper::ThreadsResume() {
nealsidde545c02010-03-02 00:39:48 +0000152 if (!threads_suspended_)
nealsidb0baafc2009-08-17 23:12:53 +0000153 return false;
154 bool good = true;
155 for (size_t i = 0; i < threads_.size(); ++i)
156 good &= ResumeThread(threads_[i]);
nealsidde545c02010-03-02 00:39:48 +0000157 threads_suspended_ = false;
nealsidb0baafc2009-08-17 23:12:53 +0000158 return good;
159}
160
161void
162LinuxDumper::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.mielczarek0a5fc5d2009-12-23 17:09:27 +0000207bool
208LinuxDumper::ElfFileIdentifierForMapping(unsigned int mapping_id,
209 uint8_t identifier[sizeof(MDGUID)])
210{
211 assert(mapping_id < mappings_.size());
nealsid6bc523c2010-05-10 20:35:54 +0000212 my_memset(identifier, 0, sizeof(MDGUID));
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000213 const MappingInfo* mapping = mappings_[mapping_id];
nealsid6bc523c2010-05-10 20:35:54 +0000214 if (IsMappedFileOpenUnsafe(mapping)) {
215 return false;
216 }
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000217 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.mielczarek9f211b42009-12-23 20:44:32 +0000225#if defined(__x86_64)
226#define sys_mmap2 sys_mmap
227#endif
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000228 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
nealsidb0baafc2009-08-17 23:12:53 +0000238void*
239LinuxDumper::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
268bool
269LinuxDumper::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.
331bool 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.
nealsidde545c02010-03-02 00:39:48 +0000362// Fill out the |tgid|, |ppid| and |pid| members of |info|. If unavailable,
nealsidb0baafc2009-08-17 23:12:53 +0000363// these members are set to -1. Returns true iff all three members are
nealsidde545c02010-03-02 00:39:48 +0000364// available.
nealsidb0baafc2009-08-17 23:12:53 +0000365bool 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.mielczarekcfc86282010-10-20 15:51:38 +0000393 if (sys_ptrace(PTRACE_GETREGS, tid, NULL, &info->regs) == -1) {
nealsidb0baafc2009-08-17 23:12:53 +0000394 return false;
395 }
396
ted.mielczarekcfc86282010-10-20 15:51:38 +0000397#if !defined(__ANDROID__)
398 if (sys_ptrace(PTRACE_GETFPREGS, tid, NULL, &info->fpregs) == -1) {
399 return false;
400 }
401#endif
402
nealsid096992f2009-12-01 21:35:52 +0000403#if defined(__i386)
nealsidb0baafc2009-08-17 23:12:53 +0000404 if (sys_ptrace(PTRACE_GETFPXREGS, tid, NULL, &info->fpxregs) == -1)
405 return false;
nealsid096992f2009-12-01 21:35:52 +0000406#endif
nealsidb0baafc2009-08-17 23:12:53 +0000407
nealsid096992f2009-12-01 21:35:52 +0000408#if defined(__i386) || defined(__x86_64)
nealsidb0baafc2009-08-17 23:12:53 +0000409 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));
nealsidde545c02010-03-02 00:39:48 +0000426#elif defined(__ARM_EABI__)
ted.mielczarekcfc86282010-10-20 15:51:38 +0000427 memcpy(&stack_pointer, &info->regs.ARM_sp, sizeof(info->regs.ARM_sp));
nealsidb0baafc2009-08-17 23:12:53 +0000428#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.
442bool LinuxDumper::GetStackInfo(const void** stack, size_t* stack_len,
443 uintptr_t int_stack_pointer) {
nealsidb0baafc2009-08-17 23:12:53 +0000444 // Move the stack pointer to the bottom of the page that it's in.
nealsidde545c02010-03-02 00:39:48 +0000445 const uintptr_t page_size = getpagesize();
446
nealsidb0baafc2009-08-17 23:12:53 +0000447 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.
nealsidbb618862009-12-10 19:15:44 +0000451 static ptrdiff_t kStackToCapture = 32 * 1024;
nealsidb0baafc2009-08-17 23:12:53 +0000452
453 const MappingInfo* mapping = FindMapping(stack_pointer);
454 if (!mapping)
455 return false;
nealsidde545c02010-03-02 00:39:48 +0000456 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;
nealsidb0baafc2009-08-17 23:12:53 +0000462 return true;
463}
464
465// static
466void LinuxDumper::CopyFromProcess(void* dest, pid_t child, const void* src,
467 size_t length) {
nealsidde545c02010-03-02 00:39:48 +0000468 unsigned long tmp = 55;
nealsidb0baafc2009-08-17 23:12:53 +0000469 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;
nealsidde545c02010-03-02 00:39:48 +0000476 if (sys_ptrace(PTRACE_PEEKDATA, child, remote + done, &tmp) == -1) {
nealsidb0baafc2009-08-17 23:12:53 +0000477 tmp = 0;
nealsidde545c02010-03-02 00:39:48 +0000478 }
nealsidb0baafc2009-08-17 23:12:53 +0000479 memcpy(local + done, &tmp, l);
480 done += l;
481 }
482}
483
484// Find the mapping which the given memory address falls in.
485const 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