~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to thirdparty/breakpad/client/linux/minidump_writer/linux_ptrace_dumper.cc

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2012, 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
// 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.
 
33
 
 
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.
 
38
 
 
39
#include "client/linux/minidump_writer/linux_ptrace_dumper.h"
 
40
 
 
41
#include <asm/ptrace.h>
 
42
#include <assert.h>
 
43
#include <errno.h>
 
44
#include <fcntl.h>
 
45
#include <limits.h>
 
46
#include <stddef.h>
 
47
#include <stdlib.h>
 
48
#include <string.h>
 
49
#include <sys/ptrace.h>
 
50
#include <sys/wait.h>
 
51
 
 
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"
 
56
 
 
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.
 
60
  errno = 0;
 
61
  if (sys_ptrace(PTRACE_ATTACH, pid, NULL, NULL) != 0 &&
 
62
      errno != 0) {
 
63
    return false;
 
64
  }
 
65
  while (sys_waitpid(pid, NULL, __WALL) < 0) {
 
66
    if (errno != EINTR) {
 
67
      sys_ptrace(PTRACE_DETACH, pid, NULL, NULL);
 
68
      return false;
 
69
    }
 
70
  }
 
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, &regs) == -1 ||
 
81
#if defined(__i386)
 
82
      !regs.esp
 
83
#elif defined(__x86_64)
 
84
      !regs.rsp
 
85
#endif
 
86
      ) {
 
87
    sys_ptrace(PTRACE_DETACH, pid, NULL, NULL);
 
88
    return false;
 
89
  }
 
90
#endif
 
91
  return true;
 
92
}
 
93
 
 
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;
 
97
}
 
98
 
 
99
namespace google_breakpad {
 
100
 
 
101
LinuxPtraceDumper::LinuxPtraceDumper(pid_t pid)
 
102
    : LinuxDumper(pid),
 
103
      threads_suspended_(false) {
 
104
}
 
105
 
 
106
bool LinuxPtraceDumper::BuildProcPath(char* path, pid_t pid,
 
107
                                      const char* node) const {
 
108
  if (!path || !node || pid <= 0)
 
109
    return false;
 
110
 
 
111
  size_t node_len = my_strlen(node);
 
112
  if (node_len == 0)
 
113
    return false;
 
114
 
 
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)
 
118
    return false;
 
119
 
 
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';
 
125
  return true;
 
126
}
 
127
 
 
128
void LinuxPtraceDumper::CopyFromProcess(void* dest, pid_t child,
 
129
                                        const void* src, size_t length) {
 
130
  unsigned long tmp = 55;
 
131
  size_t done = 0;
 
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;
 
135
 
 
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) {
 
139
      tmp = 0;
 
140
    }
 
141
    memcpy(local + done, &tmp, l);
 
142
    done += l;
 
143
  }
 
144
}
 
145
 
 
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
 
149
// available.
 
150
bool LinuxPtraceDumper::GetThreadInfoByIndex(size_t index, ThreadInfo* info) {
 
151
  if (index >= threads_.size())
 
152
    return false;
 
153
 
 
154
  pid_t tid = threads_[index];
 
155
 
 
156
  assert(info != NULL);
 
157
  char status_path[NAME_MAX];
 
158
  if (!BuildProcPath(status_path, tid, "status"))
 
159
    return false;
 
160
 
 
161
  const int fd = sys_open(status_path, O_RDONLY, 0);
 
162
  if (fd < 0)
 
163
    return false;
 
164
 
 
165
  LineReader* const line_reader = new(allocator_) LineReader(fd);
 
166
  const char* line;
 
167
  unsigned line_len;
 
168
 
 
169
  info->ppid = info->tgid = -1;
 
170
 
 
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);
 
176
    }
 
177
 
 
178
    line_reader->PopLine(line_len);
 
179
  }
 
180
  sys_close(fd);
 
181
 
 
182
  if (info->ppid == -1 || info->tgid == -1)
 
183
    return false;
 
184
 
 
185
  if (sys_ptrace(PTRACE_GETREGS, tid, NULL, &info->regs) == -1) {
 
186
    return false;
 
187
  }
 
188
 
 
189
#if !defined(__ANDROID__)
 
190
  if (sys_ptrace(PTRACE_GETFPREGS, tid, NULL, &info->fpregs) == -1) {
 
191
    return false;
 
192
  }
 
193
#endif
 
194
 
 
195
#if defined(__i386)
 
196
  if (sys_ptrace(PTRACE_GETFPXREGS, tid, NULL, &info->fpxregs) == -1)
 
197
    return false;
 
198
#endif
 
199
 
 
200
#if defined(__i386) || defined(__x86_64)
 
201
  for (unsigned i = 0; i < ThreadInfo::kNumDebugRegisters; ++i) {
 
202
    if (sys_ptrace(
 
203
        PTRACE_PEEKUSER, tid,
 
204
        reinterpret_cast<void*> (offsetof(struct user,
 
205
                                          u_debugreg[0]) + i *
 
206
                                 sizeof(debugreg_t)),
 
207
        &info->dregs[i]) == -1) {
 
208
      return false;
 
209
    }
 
210
  }
 
211
#endif
 
212
 
 
213
  const uint8_t* stack_pointer;
 
214
#if defined(__i386)
 
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));
 
220
#else
 
221
#error "This code hasn't been ported to your platform yet."
 
222
#endif
 
223
 
 
224
  return GetStackInfo(&info->stack, &info->stack_len,
 
225
                      (uintptr_t) stack_pointer);
 
226
}
 
227
 
 
228
bool LinuxPtraceDumper::IsPostMortem() const {
 
229
  return false;
 
230
}
 
231
 
 
232
bool LinuxPtraceDumper::ThreadsSuspend() {
 
233
  if (threads_suspended_)
 
234
    return true;
 
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);
 
243
      --i;
 
244
    }
 
245
  }
 
246
  threads_suspended_ = true;
 
247
  return threads_.size() > 0;
 
248
}
 
249
 
 
250
bool LinuxPtraceDumper::ThreadsResume() {
 
251
  if (!threads_suspended_)
 
252
    return false;
 
253
  bool good = true;
 
254
  for (size_t i = 0; i < threads_.size(); ++i)
 
255
    good &= ResumeThread(threads_[i]);
 
256
  threads_suspended_ = false;
 
257
  return good;
 
258
}
 
259
 
 
260
// Parse /proc/$pid/task to list all the threads of the process identified by
 
261
// pid.
 
262
bool LinuxPtraceDumper::EnumerateThreads() {
 
263
  char task_path[NAME_MAX];
 
264
  if (!BuildProcPath(task_path, pid_, "task"))
 
265
    return false;
 
266
 
 
267
  const int fd = sys_open(task_path, O_RDONLY | O_DIRECTORY, 0);
 
268
  if (fd < 0)
 
269
    return false;
 
270
  DirectoryReader* dir_reader = new(allocator_) DirectoryReader(fd);
 
271
 
 
272
  // The directory may contain duplicate entries which we filter by assuming
 
273
  // that they are consecutive.
 
274
  int last_tid = -1;
 
275
  const char* dent_name;
 
276
  while (dir_reader->GetNextEntry(&dent_name)) {
 
277
    if (my_strcmp(dent_name, ".") &&
 
278
        my_strcmp(dent_name, "..")) {
 
279
      int tid = 0;
 
280
      if (my_strtoui(&tid, dent_name) &&
 
281
          last_tid != tid) {
 
282
        last_tid = tid;
 
283
        threads_.push_back(tid);
 
284
      }
 
285
    }
 
286
    dir_reader->PopEntry();
 
287
  }
 
288
 
 
289
  sys_close(fd);
 
290
  return true;
 
291
}
 
292
 
 
293
}  // namespace google_breakpad