blob: be199f7bfc403b6b416f5006dd9c5dd03826e1e4 [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.mielczarek0a5fc5d2009-12-23 17:09:27 +000048#include <link.h>
nealsidb0baafc2009-08-17 23:12:53 +000049
50#include <sys/types.h>
51#include <sys/ptrace.h>
52#include <sys/wait.h>
53
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000054#include <algorithm>
55
nealsidb0baafc2009-08-17 23:12:53 +000056#include "client/linux/minidump_writer/directory_reader.h"
57#include "client/linux/minidump_writer/line_reader.h"
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000058#include "common/linux/file_id.h"
nealsidb0baafc2009-08-17 23:12:53 +000059#include "common/linux/linux_libc_support.h"
60#include "common/linux/linux_syscall_support.h"
61
62// Suspend a thread by attaching to it.
63static bool SuspendThread(pid_t pid) {
64 // This may fail if the thread has just died or debugged.
65 errno = 0;
66 if (sys_ptrace(PTRACE_ATTACH, pid, NULL, NULL) != 0 &&
67 errno != 0) {
68 return false;
69 }
70 while (sys_waitpid(pid, NULL, __WALL) < 0) {
71 if (errno != EINTR) {
72 sys_ptrace(PTRACE_DETACH, pid, NULL, NULL);
73 return false;
74 }
75 }
76 return true;
77}
78
79// Resume a thread by detaching from it.
80static bool ResumeThread(pid_t pid) {
81 return sys_ptrace(PTRACE_DETACH, pid, NULL, NULL) >= 0;
82}
83
84namespace google_breakpad {
85
86LinuxDumper::LinuxDumper(int pid)
87 : pid_(pid),
nealsidde545c02010-03-02 00:39:48 +000088 threads_suspended_(false),
nealsidb0baafc2009-08-17 23:12:53 +000089 threads_(&allocator_, 8),
90 mappings_(&allocator_) {
91}
92
93bool LinuxDumper::Init() {
94 return EnumerateThreads(&threads_) &&
95 EnumerateMappings(&mappings_);
96}
97
98bool LinuxDumper::ThreadsSuspend() {
nealsidde545c02010-03-02 00:39:48 +000099 if (threads_suspended_)
nealsidb0baafc2009-08-17 23:12:53 +0000100 return true;
101 bool good = true;
102 for (size_t i = 0; i < threads_.size(); ++i)
103 good &= SuspendThread(threads_[i]);
nealsidde545c02010-03-02 00:39:48 +0000104 threads_suspended_ = true;
nealsidb0baafc2009-08-17 23:12:53 +0000105 return good;
106}
107
108bool LinuxDumper::ThreadsResume() {
nealsidde545c02010-03-02 00:39:48 +0000109 if (!threads_suspended_)
nealsidb0baafc2009-08-17 23:12:53 +0000110 return false;
111 bool good = true;
112 for (size_t i = 0; i < threads_.size(); ++i)
113 good &= ResumeThread(threads_[i]);
nealsidde545c02010-03-02 00:39:48 +0000114 threads_suspended_ = false;
nealsidb0baafc2009-08-17 23:12:53 +0000115 return good;
116}
117
118void
119LinuxDumper::BuildProcPath(char* path, pid_t pid, const char* node) const {
120 assert(path);
121 if (!path) {
122 return;
123 }
124
125 path[0] = '\0';
126
127 const unsigned pid_len = my_int_len(pid);
128
129 assert(node);
130 if (!node) {
131 return;
132 }
133
134 size_t node_len = my_strlen(node);
135 assert(node_len < NAME_MAX);
136 if (node_len >= NAME_MAX) {
137 return;
138 }
139
140 assert(node_len > 0);
141 if (node_len == 0) {
142 return;
143 }
144
145 assert(pid > 0);
146 if (pid <= 0) {
147 return;
148 }
149
150 const size_t total_length = 6 + pid_len + 1 + node_len;
151
152 assert(total_length < NAME_MAX);
153 if (total_length >= NAME_MAX) {
154 return;
155 }
156
157 memcpy(path, "/proc/", 6);
158 my_itos(path + 6, pid, pid_len);
159 memcpy(path + 6 + pid_len, "/", 1);
160 memcpy(path + 6 + pid_len + 1, node, node_len);
161 memcpy(path + total_length, "\0", 1);
162}
163
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000164bool
165LinuxDumper::ElfFileIdentifierForMapping(unsigned int mapping_id,
166 uint8_t identifier[sizeof(MDGUID)])
167{
168 assert(mapping_id < mappings_.size());
169 const MappingInfo* mapping = mappings_[mapping_id];
170 int fd = sys_open(mapping->name, O_RDONLY, 0);
171 if (fd < 0)
172 return false;
173 struct kernel_stat st;
174 if (sys_fstat(fd, &st) != 0) {
175 sys_close(fd);
176 return false;
177 }
ted.mielczarek9f211b42009-12-23 20:44:32 +0000178#if defined(__x86_64)
179#define sys_mmap2 sys_mmap
180#endif
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000181 void* base = sys_mmap2(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
182 sys_close(fd);
183 if (base == MAP_FAILED)
184 return false;
185
186 bool success = FileID::ElfFileIdentifierFromMappedFile(base, identifier);
187 sys_munmap(base, st.st_size);
188 return success;
189}
190
nealsidb0baafc2009-08-17 23:12:53 +0000191void*
192LinuxDumper::FindBeginningOfLinuxGateSharedLibrary(const pid_t pid) const {
193 char auxv_path[80];
194 BuildProcPath(auxv_path, pid, "auxv");
195
196 // If BuildProcPath errors out due to invalid input, we'll handle it when
197 // we try to sys_open the file.
198
199 // Find the AT_SYSINFO_EHDR entry for linux-gate.so
200 // See http://www.trilithium.com/johan/2005/08/linux-gate/ for more
201 // information.
202 int fd = sys_open(auxv_path, O_RDONLY, 0);
203 if (fd < 0) {
204 return NULL;
205 }
206
207 elf_aux_entry one_aux_entry;
208 while (sys_read(fd,
209 &one_aux_entry,
210 sizeof(elf_aux_entry)) == sizeof(elf_aux_entry) &&
211 one_aux_entry.a_type != AT_NULL) {
212 if (one_aux_entry.a_type == AT_SYSINFO_EHDR) {
213 close(fd);
214 return reinterpret_cast<void*>(one_aux_entry.a_un.a_val);
215 }
216 }
217 close(fd);
218 return NULL;
219}
220
221bool
222LinuxDumper::EnumerateMappings(wasteful_vector<MappingInfo*>* result) const {
223 char maps_path[80];
224 BuildProcPath(maps_path, pid_, "maps");
225
226 // linux_gate_loc is the beginning of the kernel's mapping of
227 // linux-gate.so in the process. It doesn't actually show up in the
228 // maps list as a filename, so we use the aux vector to find it's
229 // load location and special case it's entry when creating the list
230 // of mappings.
231 const void* linux_gate_loc;
232 linux_gate_loc = FindBeginningOfLinuxGateSharedLibrary(pid_);
233
234 const int fd = sys_open(maps_path, O_RDONLY, 0);
235 if (fd < 0)
236 return false;
237 LineReader* const line_reader = new(allocator_) LineReader(fd);
238
239 const char* line;
240 unsigned line_len;
241 while (line_reader->GetNextLine(&line, &line_len)) {
242 uintptr_t start_addr, end_addr, offset;
243
244 const char* i1 = my_read_hex_ptr(&start_addr, line);
245 if (*i1 == '-') {
246 const char* i2 = my_read_hex_ptr(&end_addr, i1 + 1);
247 if (*i2 == ' ') {
248 const char* i3 = my_read_hex_ptr(&offset, i2 + 6 /* skip ' rwxp ' */);
249 if (*i3 == ' ') {
250 MappingInfo* const module = new(allocator_) MappingInfo;
251 memset(module, 0, sizeof(MappingInfo));
252 module->start_addr = start_addr;
253 module->size = end_addr - start_addr;
254 module->offset = offset;
255 const char* name = NULL;
256 // Only copy name if the name is a valid path name, or if
257 // we've found the VDSO image
258 if ((name = my_strchr(line, '/')) != NULL) {
259 const unsigned l = my_strlen(name);
260 if (l < sizeof(module->name))
261 memcpy(module->name, name, l);
262 } else if (linux_gate_loc &&
263 reinterpret_cast<void*>(module->start_addr) ==
264 linux_gate_loc) {
265 memcpy(module->name,
266 kLinuxGateLibraryName,
267 my_strlen(kLinuxGateLibraryName));
268 module->offset = 0;
269 }
270 result->push_back(module);
271 }
272 }
273 }
274 line_reader->PopLine(line_len);
275 }
276
277 sys_close(fd);
278
279 return result->size() > 0;
280}
281
282// Parse /proc/$pid/task to list all the threads of the process identified by
283// pid.
284bool LinuxDumper::EnumerateThreads(wasteful_vector<pid_t>* result) const {
285 char task_path[80];
286 BuildProcPath(task_path, pid_, "task");
287
288 const int fd = sys_open(task_path, O_RDONLY | O_DIRECTORY, 0);
289 if (fd < 0)
290 return false;
291 DirectoryReader* dir_reader = new(allocator_) DirectoryReader(fd);
292
293 // The directory may contain duplicate entries which we filter by assuming
294 // that they are consecutive.
295 int last_tid = -1;
296 const char* dent_name;
297 while (dir_reader->GetNextEntry(&dent_name)) {
298 if (my_strcmp(dent_name, ".") &&
299 my_strcmp(dent_name, "..")) {
300 int tid = 0;
301 if (my_strtoui(&tid, dent_name) &&
302 last_tid != tid) {
303 last_tid = tid;
304 result->push_back(tid);
305 }
306 }
307 dir_reader->PopEntry();
308 }
309
310 sys_close(fd);
311 return true;
312}
313
314// Read thread info from /proc/$pid/status.
nealsidde545c02010-03-02 00:39:48 +0000315// Fill out the |tgid|, |ppid| and |pid| members of |info|. If unavailable,
nealsidb0baafc2009-08-17 23:12:53 +0000316// these members are set to -1. Returns true iff all three members are
nealsidde545c02010-03-02 00:39:48 +0000317// available.
nealsidb0baafc2009-08-17 23:12:53 +0000318bool LinuxDumper::ThreadInfoGet(pid_t tid, ThreadInfo* info) {
319 assert(info != NULL);
320 char status_path[80];
321 BuildProcPath(status_path, tid, "status");
322
323 const int fd = open(status_path, O_RDONLY);
324 if (fd < 0)
325 return false;
326
327 LineReader* const line_reader = new(allocator_) LineReader(fd);
328 const char* line;
329 unsigned line_len;
330
331 info->ppid = info->tgid = -1;
332
333 while (line_reader->GetNextLine(&line, &line_len)) {
334 if (my_strncmp("Tgid:\t", line, 6) == 0) {
335 my_strtoui(&info->tgid, line + 6);
336 } else if (my_strncmp("PPid:\t", line, 6) == 0) {
337 my_strtoui(&info->ppid, line + 6);
338 }
339
340 line_reader->PopLine(line_len);
341 }
342
343 if (info->ppid == -1 || info->tgid == -1)
344 return false;
345
346 if (sys_ptrace(PTRACE_GETREGS, tid, NULL, &info->regs) == -1 ||
347 sys_ptrace(PTRACE_GETFPREGS, tid, NULL, &info->fpregs) == -1) {
348 return false;
349 }
350
nealsid096992f2009-12-01 21:35:52 +0000351#if defined(__i386)
nealsidb0baafc2009-08-17 23:12:53 +0000352 if (sys_ptrace(PTRACE_GETFPXREGS, tid, NULL, &info->fpxregs) == -1)
353 return false;
nealsid096992f2009-12-01 21:35:52 +0000354#endif
nealsidb0baafc2009-08-17 23:12:53 +0000355
nealsid096992f2009-12-01 21:35:52 +0000356#if defined(__i386) || defined(__x86_64)
nealsidb0baafc2009-08-17 23:12:53 +0000357 for (unsigned i = 0; i < ThreadInfo::kNumDebugRegisters; ++i) {
358 if (sys_ptrace(
359 PTRACE_PEEKUSER, tid,
360 reinterpret_cast<void*> (offsetof(struct user,
361 u_debugreg[0]) + i *
362 sizeof(debugreg_t)),
363 &info->dregs[i]) == -1) {
364 return false;
365 }
366 }
367#endif
368
369 const uint8_t* stack_pointer;
370#if defined(__i386)
371 memcpy(&stack_pointer, &info->regs.esp, sizeof(info->regs.esp));
372#elif defined(__x86_64)
373 memcpy(&stack_pointer, &info->regs.rsp, sizeof(info->regs.rsp));
nealsidde545c02010-03-02 00:39:48 +0000374#elif defined(__ARM_EABI__)
375 memcpy(&stack_pointer, &info->regs.uregs[R13], sizeof(info->regs.uregs[R13]));
nealsidb0baafc2009-08-17 23:12:53 +0000376#else
377#error "This code hasn't been ported to your platform yet."
378#endif
379
380 if (!GetStackInfo(&info->stack, &info->stack_len,
381 (uintptr_t) stack_pointer))
382 return false;
383
384 return true;
385}
386
387// Get information about the stack, given the stack pointer. We don't try to
388// walk the stack since we might not have all the information needed to do
389// unwind. So we just grab, up to, 32k of stack.
390bool LinuxDumper::GetStackInfo(const void** stack, size_t* stack_len,
391 uintptr_t int_stack_pointer) {
nealsidb0baafc2009-08-17 23:12:53 +0000392 // Move the stack pointer to the bottom of the page that it's in.
nealsidde545c02010-03-02 00:39:48 +0000393 const uintptr_t page_size = getpagesize();
394
nealsidb0baafc2009-08-17 23:12:53 +0000395 uint8_t* const stack_pointer =
396 reinterpret_cast<uint8_t*>(int_stack_pointer & ~(page_size - 1));
397
398 // The number of bytes of stack which we try to capture.
nealsidbb618862009-12-10 19:15:44 +0000399 static ptrdiff_t kStackToCapture = 32 * 1024;
nealsidb0baafc2009-08-17 23:12:53 +0000400
401 const MappingInfo* mapping = FindMapping(stack_pointer);
402 if (!mapping)
403 return false;
nealsidde545c02010-03-02 00:39:48 +0000404 const ptrdiff_t offset = stack_pointer - (uint8_t*) mapping->start_addr;
405 const ptrdiff_t distance_to_end =
406 static_cast<ptrdiff_t>(mapping->size) - offset;
407 *stack_len = distance_to_end > kStackToCapture ?
408 kStackToCapture : distance_to_end;
409 *stack = stack_pointer;
nealsidb0baafc2009-08-17 23:12:53 +0000410 return true;
411}
412
413// static
414void LinuxDumper::CopyFromProcess(void* dest, pid_t child, const void* src,
415 size_t length) {
nealsidde545c02010-03-02 00:39:48 +0000416 unsigned long tmp = 55;
nealsidb0baafc2009-08-17 23:12:53 +0000417 size_t done = 0;
418 static const size_t word_size = sizeof(tmp);
419 uint8_t* const local = (uint8_t*) dest;
420 uint8_t* const remote = (uint8_t*) src;
421
422 while (done < length) {
423 const size_t l = length - done > word_size ? word_size : length - done;
nealsidde545c02010-03-02 00:39:48 +0000424 if (sys_ptrace(PTRACE_PEEKDATA, child, remote + done, &tmp) == -1) {
nealsidb0baafc2009-08-17 23:12:53 +0000425 tmp = 0;
nealsidde545c02010-03-02 00:39:48 +0000426 }
nealsidb0baafc2009-08-17 23:12:53 +0000427 memcpy(local + done, &tmp, l);
428 done += l;
429 }
430}
431
432// Find the mapping which the given memory address falls in.
433const MappingInfo* LinuxDumper::FindMapping(const void* address) const {
434 const uintptr_t addr = (uintptr_t) address;
435
436 for (size_t i = 0; i < mappings_.size(); ++i) {
437 const uintptr_t start = static_cast<uintptr_t>(mappings_[i]->start_addr);
438 if (addr >= start && addr - start < mappings_[i]->size)
439 return mappings_[i];
440 }
441
442 return NULL;
443}
444
445} // namespace google_breakpad