1
// Copyright (c) 2012, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// linux_ptrace_dumper.cc: Implement google_breakpad::LinuxPtraceDumper.
31
// See linux_ptrace_dumper.h for detals.
32
// This class was originally splitted from google_breakpad::LinuxDumper.
34
// This code deals with the mechanics of getting information about a crashed
35
// process. Since this code may run in a compromised address space, the same
36
// rules apply as detailed at the top of minidump_writer.h: no libc calls and
37
// use the alternative allocator.
39
#include "client/linux/minidump_writer/linux_ptrace_dumper.h"
41
#include <asm/ptrace.h>
49
#include <sys/ptrace.h>
52
#include "client/linux/minidump_writer/directory_reader.h"
53
#include "client/linux/minidump_writer/line_reader.h"
54
#include "common/linux/linux_libc_support.h"
55
#include "third_party/lss/linux_syscall_support.h"
57
// Suspends a thread by attaching to it.
58
static bool SuspendThread(pid_t pid) {
59
// This may fail if the thread has just died or debugged.
61
if (sys_ptrace(PTRACE_ATTACH, pid, NULL, NULL) != 0 &&
65
while (sys_waitpid(pid, NULL, __WALL) < 0) {
67
sys_ptrace(PTRACE_DETACH, pid, NULL, NULL);
71
#if defined(__i386) || defined(__x86_64)
72
// On x86, the stack pointer is NULL or -1, when executing trusted code in
73
// the seccomp sandbox. Not only does this cause difficulties down the line
74
// when trying to dump the thread's stack, it also results in the minidumps
75
// containing information about the trusted threads. This information is
76
// generally completely meaningless and just pollutes the minidumps.
77
// We thus test the stack pointer and exclude any threads that are part of
78
// the seccomp sandbox's trusted code.
79
user_regs_struct regs;
80
if (sys_ptrace(PTRACE_GETREGS, pid, NULL, ®s) == -1 ||
83
#elif defined(__x86_64)
87
sys_ptrace(PTRACE_DETACH, pid, NULL, NULL);
94
// Resumes a thread by detaching from it.
95
static bool ResumeThread(pid_t pid) {
96
return sys_ptrace(PTRACE_DETACH, pid, NULL, NULL) >= 0;
99
namespace google_breakpad {
101
LinuxPtraceDumper::LinuxPtraceDumper(pid_t pid)
103
threads_suspended_(false) {
106
bool LinuxPtraceDumper::BuildProcPath(char* path, pid_t pid,
107
const char* node) const {
108
if (!path || !node || pid <= 0)
111
size_t node_len = my_strlen(node);
115
const unsigned pid_len = my_int_len(pid);
116
const size_t total_length = 6 + pid_len + 1 + node_len;
117
if (total_length >= NAME_MAX)
120
memcpy(path, "/proc/", 6);
121
my_itos(path + 6, pid, pid_len);
122
path[6 + pid_len] = '/';
123
memcpy(path + 6 + pid_len + 1, node, node_len);
124
path[total_length] = '\0';
128
void LinuxPtraceDumper::CopyFromProcess(void* dest, pid_t child,
129
const void* src, size_t length) {
130
unsigned long tmp = 55;
132
static const size_t word_size = sizeof(tmp);
133
uint8_t* const local = (uint8_t*) dest;
134
uint8_t* const remote = (uint8_t*) src;
136
while (done < length) {
137
const size_t l = (length - done > word_size) ? word_size : (length - done);
138
if (sys_ptrace(PTRACE_PEEKDATA, child, remote + done, &tmp) == -1) {
141
memcpy(local + done, &tmp, l);
146
// Read thread info from /proc/$pid/status.
147
// Fill out the |tgid|, |ppid| and |pid| members of |info|. If unavailable,
148
// these members are set to -1. Returns true iff all three members are
150
bool LinuxPtraceDumper::GetThreadInfoByIndex(size_t index, ThreadInfo* info) {
151
if (index >= threads_.size())
154
pid_t tid = threads_[index];
156
assert(info != NULL);
157
char status_path[NAME_MAX];
158
if (!BuildProcPath(status_path, tid, "status"))
161
const int fd = sys_open(status_path, O_RDONLY, 0);
165
LineReader* const line_reader = new(allocator_) LineReader(fd);
169
info->ppid = info->tgid = -1;
171
while (line_reader->GetNextLine(&line, &line_len)) {
172
if (my_strncmp("Tgid:\t", line, 6) == 0) {
173
my_strtoui(&info->tgid, line + 6);
174
} else if (my_strncmp("PPid:\t", line, 6) == 0) {
175
my_strtoui(&info->ppid, line + 6);
178
line_reader->PopLine(line_len);
182
if (info->ppid == -1 || info->tgid == -1)
185
if (sys_ptrace(PTRACE_GETREGS, tid, NULL, &info->regs) == -1) {
189
#if !defined(__ANDROID__)
190
if (sys_ptrace(PTRACE_GETFPREGS, tid, NULL, &info->fpregs) == -1) {
196
if (sys_ptrace(PTRACE_GETFPXREGS, tid, NULL, &info->fpxregs) == -1)
200
#if defined(__i386) || defined(__x86_64)
201
for (unsigned i = 0; i < ThreadInfo::kNumDebugRegisters; ++i) {
203
PTRACE_PEEKUSER, tid,
204
reinterpret_cast<void*> (offsetof(struct user,
207
&info->dregs[i]) == -1) {
213
const uint8_t* stack_pointer;
215
memcpy(&stack_pointer, &info->regs.esp, sizeof(info->regs.esp));
216
#elif defined(__x86_64)
217
memcpy(&stack_pointer, &info->regs.rsp, sizeof(info->regs.rsp));
218
#elif defined(__ARM_EABI__)
219
memcpy(&stack_pointer, &info->regs.ARM_sp, sizeof(info->regs.ARM_sp));
221
#error "This code hasn't been ported to your platform yet."
224
return GetStackInfo(&info->stack, &info->stack_len,
225
(uintptr_t) stack_pointer);
228
bool LinuxPtraceDumper::IsPostMortem() const {
232
bool LinuxPtraceDumper::ThreadsSuspend() {
233
if (threads_suspended_)
235
for (size_t i = 0; i < threads_.size(); ++i) {
236
if (!SuspendThread(threads_[i])) {
237
// If the thread either disappeared before we could attach to it, or if
238
// it was part of the seccomp sandbox's trusted code, it is OK to
239
// silently drop it from the minidump.
240
memmove(&threads_[i], &threads_[i+1],
241
(threads_.size() - i - 1) * sizeof(threads_[i]));
242
threads_.resize(threads_.size() - 1);
246
threads_suspended_ = true;
247
return threads_.size() > 0;
250
bool LinuxPtraceDumper::ThreadsResume() {
251
if (!threads_suspended_)
254
for (size_t i = 0; i < threads_.size(); ++i)
255
good &= ResumeThread(threads_[i]);
256
threads_suspended_ = false;
260
// Parse /proc/$pid/task to list all the threads of the process identified by
262
bool LinuxPtraceDumper::EnumerateThreads() {
263
char task_path[NAME_MAX];
264
if (!BuildProcPath(task_path, pid_, "task"))
267
const int fd = sys_open(task_path, O_RDONLY | O_DIRECTORY, 0);
270
DirectoryReader* dir_reader = new(allocator_) DirectoryReader(fd);
272
// The directory may contain duplicate entries which we filter by assuming
273
// that they are consecutive.
275
const char* dent_name;
276
while (dir_reader->GetNextEntry(&dent_name)) {
277
if (my_strcmp(dent_name, ".") &&
278
my_strcmp(dent_name, "..")) {
280
if (my_strtoui(&tid, dent_name) &&
283
threads_.push_back(tid);
286
dir_reader->PopEntry();
293
} // namespace google_breakpad