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