PageRenderTime 26ms CodeModel.GetById 9ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/client/linux/minidump_writer/linux_dumper.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 235 lines | 108 code | 42 blank | 85 comment | 3 complexity | cc1a0aaeb074480221688f52cd2dc4fb MD5 | raw file
  1// Copyright (c) 2010, 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_dumper.h: Define the google_breakpad::LinuxDumper class, which
 31// is a base class for extracting information of a crashed process. It
 32// was originally a complete implementation using the ptrace API, but
 33// has been refactored to allow derived implementations supporting both
 34// ptrace and core dump. A portion of the original implementation is now
 35// in google_breakpad::LinuxPtraceDumper (see linux_ptrace_dumper.h for
 36// details).
 37
 38#ifndef CLIENT_LINUX_MINIDUMP_WRITER_LINUX_DUMPER_H_
 39#define CLIENT_LINUX_MINIDUMP_WRITER_LINUX_DUMPER_H_
 40
 41#include <elf.h>
 42#include <linux/limits.h>
 43#include <stdint.h>
 44#include <sys/types.h>
 45#if !defined(__ANDROID__)
 46#include <sys/user.h>
 47#endif
 48
 49#include "common/memory.h"
 50#include "google_breakpad/common/minidump_format.h"
 51
 52namespace google_breakpad {
 53
 54#if defined(__i386) || defined(__x86_64)
 55typedef typeof(((struct user*) 0)->u_debugreg[0]) debugreg_t;
 56#endif
 57
 58// Typedef for our parsing of the auxv variables in /proc/pid/auxv.
 59#if defined(__i386) || defined(__ARM_EABI__)
 60#if !defined(__ANDROID__)
 61typedef Elf32_auxv_t elf_aux_entry;
 62#else
 63// Android is missing this structure definition
 64typedef struct
 65{
 66  uint32_t a_type;              /* Entry type */
 67  union
 68    {
 69      uint32_t a_val;           /* Integer value */
 70    } a_un;
 71} elf_aux_entry;
 72
 73#if !defined(AT_SYSINFO_EHDR)
 74#define AT_SYSINFO_EHDR 33
 75#endif
 76#endif  // __ANDROID__
 77#elif defined(__x86_64)
 78typedef Elf64_auxv_t elf_aux_entry;
 79#endif
 80// When we find the VDSO mapping in the process's address space, this
 81// is the name we use for it when writing it to the minidump.
 82// This should always be less than NAME_MAX!
 83const char kLinuxGateLibraryName[] = "linux-gate.so";
 84
 85// We produce one of these structures for each thread in the crashed process.
 86struct ThreadInfo {
 87  pid_t tgid;   // thread group id
 88  pid_t ppid;   // parent process
 89
 90  // Even on platforms where the stack grows down, the following will point to
 91  // the smallest address in the stack.
 92  const void* stack;  // pointer to the stack area
 93  size_t stack_len;  // length of the stack to copy
 94
 95
 96#if defined(__i386) || defined(__x86_64)
 97  user_regs_struct regs;
 98  user_fpregs_struct fpregs;
 99  static const unsigned kNumDebugRegisters = 8;
100  debugreg_t dregs[8];
101#if defined(__i386)
102  user_fpxregs_struct fpxregs;
103#endif  // defined(__i386)
104
105#elif defined(__ARM_EABI__)
106  // Mimicking how strace does this(see syscall.c, search for GETREGS)
107#if defined(__ANDROID__)
108  struct pt_regs regs;
109#else
110  struct user_regs regs;
111  struct user_fpregs fpregs;
112#endif  // __ANDROID__
113#endif
114};
115
116// One of these is produced for each mapping in the process (i.e. line in
117// /proc/$x/maps).
118struct MappingInfo {
119  uintptr_t start_addr;
120  size_t size;
121  size_t offset;  // offset into the backed file.
122  char name[NAME_MAX];
123};
124
125class LinuxDumper {
126 public:
127  explicit LinuxDumper(pid_t pid);
128
129  virtual ~LinuxDumper();
130
131  // Parse the data for |threads| and |mappings|.
132  virtual bool Init();
133
134  // Return true if the dumper performs a post-mortem dump.
135  virtual bool IsPostMortem() const = 0;
136
137  // Suspend/resume all threads in the given process.
138  virtual bool ThreadsSuspend() = 0;
139  virtual bool ThreadsResume() = 0;
140
141  // Read information about the |index|-th thread of |threads_|.
142  // Returns true on success. One must have called |ThreadsSuspend| first.
143  virtual bool GetThreadInfoByIndex(size_t index, ThreadInfo* info) = 0;
144
145  // These are only valid after a call to |Init|.
146  const wasteful_vector<pid_t> &threads() { return threads_; }
147  const wasteful_vector<MappingInfo*> &mappings() { return mappings_; }
148  const MappingInfo* FindMapping(const void* address) const;
149
150  // Find a block of memory to take as the stack given the top of stack pointer.
151  //   stack: (output) the lowest address in the memory area
152  //   stack_len: (output) the length of the memory area
153  //   stack_top: the current top of the stack
154  bool GetStackInfo(const void** stack, size_t* stack_len, uintptr_t stack_top);
155
156  PageAllocator* allocator() { return &allocator_; }
157
158  // Copy content of |length| bytes from a given process |child|,
159  // starting from |src|, into |dest|.
160  virtual void CopyFromProcess(void* dest, pid_t child, const void* src,
161                               size_t length) = 0;
162
163  // Builds a proc path for a certain pid for a node (/proc/<pid>/<node>).
164  // |path| is a character array of at least NAME_MAX bytes to return the
165  // result.|node| is the final node without any slashes. Returns true on
166  // success.
167  virtual bool BuildProcPath(char* path, pid_t pid, const char* node) const = 0;
168
169  // Generate a File ID from the .text section of a mapped entry.
170  // If not a member, mapping_id is ignored.
171  bool ElfFileIdentifierForMapping(const MappingInfo& mapping,
172                                   bool member,
173                                   unsigned int mapping_id,
174                                   uint8_t identifier[sizeof(MDGUID)]);
175
176  // Utility method to find the location of where the kernel has
177  // mapped linux-gate.so in memory(shows up in /proc/pid/maps as
178  // [vdso], but we can't guarantee that it's the only virtual dynamic
179  // shared object.  Parsing the auxilary vector for AT_SYSINFO_EHDR
180  // is the safest way to go.)
181  void* FindBeginningOfLinuxGateSharedLibrary(pid_t pid) const;
182  // Utility method to find the entry point location.
183  void* FindEntryPoint(pid_t pid) const;
184
185  uintptr_t crash_address() const { return crash_address_; }
186  void set_crash_address(uintptr_t crash_address) {
187    crash_address_ = crash_address;
188  }
189
190  int crash_signal() const { return crash_signal_; }
191  void set_crash_signal(int crash_signal) { crash_signal_ = crash_signal; }
192
193  pid_t crash_thread() const { return crash_thread_; }
194  void set_crash_thread(pid_t crash_thread) { crash_thread_ = crash_thread; }
195
196 protected:
197  virtual bool EnumerateMappings();
198
199  virtual bool EnumerateThreads() = 0;
200
201  // For the case where a running program has been deleted, it'll show up in
202  // /proc/pid/maps as "/path/to/program (deleted)". If this is the case, then
203  // see if '/path/to/program (deleted)' matches /proc/pid/exe and return
204  // /proc/pid/exe in |path| so ELF identifier generation works correctly. This
205  // also checks to see if '/path/to/program (deleted)' exists, so it does not
206  // get fooled by a poorly named binary.
207  // For programs that don't end with ' (deleted)', this is a no-op.
208  // This assumes |path| is a buffer with length NAME_MAX.
209  // Returns true if |path| is modified.
210  bool HandleDeletedFileInMapping(char* path) const;
211
212   // ID of the crashed process.
213  const pid_t pid_;
214
215  // Virtual address at which the process crashed.
216  uintptr_t crash_address_;
217
218  // Signal that terminated the crashed process.
219  int crash_signal_;
220
221  // ID of the crashed thread.
222  pid_t crash_thread_;
223
224  mutable PageAllocator allocator_;
225
226  // IDs of all the threads.
227  wasteful_vector<pid_t> threads_;
228
229  // Info from /proc/<pid>/maps.
230  wasteful_vector<MappingInfo*> mappings_;
231};
232
233}  // namespace google_breakpad
234
235#endif  // CLIENT_LINUX_HANDLER_LINUX_DUMPER_H_