Planeshift
linux_dumper.h
Go to the documentation of this 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 #include <sys/user.h>
46 
47 #include "common/memory.h"
49 
50 namespace google_breakpad {
51 
52 #if defined(__i386) || defined(__x86_64)
53 typedef typeof(((struct user*) 0)->u_debugreg[0]) debugreg_t;
54 #endif
55 
56 // Typedef for our parsing of the auxv variables in /proc/pid/auxv.
57 #if defined(__i386) || defined(__ARM_EABI__)
58 typedef Elf32_auxv_t elf_aux_entry;
59 #elif defined(__x86_64)
60 typedef Elf64_auxv_t elf_aux_entry;
61 #endif
62 
63 typedef typeof(((elf_aux_entry*) 0)->a_un.a_val) elf_aux_val_t;
64 
65 // When we find the VDSO mapping in the process's address space, this
66 // is the name we use for it when writing it to the minidump.
67 // This should always be less than NAME_MAX!
68 const char kLinuxGateLibraryName[] = "linux-gate.so";
69 
70 // We produce one of these structures for each thread in the crashed process.
71 struct ThreadInfo {
72  pid_t tgid; // thread group id
73  pid_t ppid; // parent process
74 
75  uintptr_t stack_pointer; // thread stack pointer
76 
77 
78 #if defined(__i386) || defined(__x86_64)
79  user_regs_struct regs;
80  user_fpregs_struct fpregs;
81  static const unsigned kNumDebugRegisters = 8;
82  debugreg_t dregs[8];
83 #if defined(__i386)
84  user_fpxregs_struct fpxregs;
85 #endif // defined(__i386)
86 
87 #elif defined(__ARM_EABI__)
88  // Mimicking how strace does this(see syscall.c, search for GETREGS)
89  struct user_regs regs;
90  struct user_fpregs fpregs;
91 #endif
92 };
93 
94 // One of these is produced for each mapping in the process (i.e. line in
95 // /proc/$x/maps).
96 struct MappingInfo {
97  uintptr_t start_addr;
98  size_t size;
99  size_t offset; // offset into the backed file.
100  char name[NAME_MAX];
101 };
102 
103 class LinuxDumper {
104  public:
105  explicit LinuxDumper(pid_t pid);
106 
107  virtual ~LinuxDumper();
108 
109  // Parse the data for |threads| and |mappings|.
110  virtual bool Init();
111 
112  // Return true if the dumper performs a post-mortem dump.
113  virtual bool IsPostMortem() const = 0;
114 
115  // Suspend/resume all threads in the given process.
116  virtual bool ThreadsSuspend() = 0;
117  virtual bool ThreadsResume() = 0;
118 
119  // Read information about the |index|-th thread of |threads_|.
120  // Returns true on success. One must have called |ThreadsSuspend| first.
121  virtual bool GetThreadInfoByIndex(size_t index, ThreadInfo* info) = 0;
122 
123  // These are only valid after a call to |Init|.
124  const wasteful_vector<pid_t> &threads() { return threads_; }
125  const wasteful_vector<MappingInfo*> &mappings() { return mappings_; }
126  const MappingInfo* FindMapping(const void* address) const;
127  const wasteful_vector<elf_aux_val_t>& auxv() { return auxv_; }
128 
129  // Find a block of memory to take as the stack given the top of stack pointer.
130  // stack: (output) the lowest address in the memory area
131  // stack_len: (output) the length of the memory area
132  // stack_top: the current top of the stack
133  bool GetStackInfo(const void** stack, size_t* stack_len, uintptr_t stack_top);
134 
135  PageAllocator* allocator() { return &allocator_; }
136 
137  // Copy content of |length| bytes from a given process |child|,
138  // starting from |src|, into |dest|.
139  virtual void CopyFromProcess(void* dest, pid_t child, const void* src,
140  size_t length) = 0;
141 
142  // Builds a proc path for a certain pid for a node (/proc/<pid>/<node>).
143  // |path| is a character array of at least NAME_MAX bytes to return the
144  // result.|node| is the final node without any slashes. Returns true on
145  // success.
146  virtual bool BuildProcPath(char* path, pid_t pid, const char* node) const = 0;
147 
148  // Generate a File ID from the .text section of a mapped entry.
149  // If not a member, mapping_id is ignored.
150  bool ElfFileIdentifierForMapping(const MappingInfo& mapping,
151  bool member,
152  unsigned int mapping_id,
153  uint8_t identifier[sizeof(MDGUID)]);
154 
155  uintptr_t crash_address() const { return crash_address_; }
156  void set_crash_address(uintptr_t crash_address) {
157  crash_address_ = crash_address;
158  }
159 
160  int crash_signal() const { return crash_signal_; }
161  void set_crash_signal(int crash_signal) { crash_signal_ = crash_signal; }
162 
163  pid_t crash_thread() const { return crash_thread_; }
164  void set_crash_thread(pid_t crash_thread) { crash_thread_ = crash_thread; }
165 
166  protected:
167  bool ReadAuxv();
168 
169  virtual bool EnumerateMappings();
170 
171  virtual bool EnumerateThreads() = 0;
172 
173  // For the case where a running program has been deleted, it'll show up in
174  // /proc/pid/maps as "/path/to/program (deleted)". If this is the case, then
175  // see if '/path/to/program (deleted)' matches /proc/pid/exe and return
176  // /proc/pid/exe in |path| so ELF identifier generation works correctly. This
177  // also checks to see if '/path/to/program (deleted)' exists, so it does not
178  // get fooled by a poorly named binary.
179  // For programs that don't end with ' (deleted)', this is a no-op.
180  // This assumes |path| is a buffer with length NAME_MAX.
181  // Returns true if |path| is modified.
182  bool HandleDeletedFileInMapping(char* path) const;
183 
184  // ID of the crashed process.
185  const pid_t pid_;
186 
187  // Virtual address at which the process crashed.
188  uintptr_t crash_address_;
189 
190  // Signal that terminated the crashed process.
192 
193  // ID of the crashed thread.
195 
197 
198  // IDs of all the threads.
200 
201  // Info from /proc/<pid>/maps.
203 
204  // Info from /proc/<pid>/auxv
206 };
207 
208 } // namespace google_breakpad
209 
210 #endif // CLIENT_LINUX_HANDLER_LINUX_DUMPER_H_
wasteful_vector< MappingInfo * > mappings_
Definition: linux_dumper.h:202
const wasteful_vector< MappingInfo * > & mappings()
Definition: linux_dumper.h:125
PageAllocator * allocator()
Definition: linux_dumper.h:135
const char kLinuxGateLibraryName[]
Definition: linux_dumper.h:68
const wasteful_vector< pid_t > & threads()
Definition: linux_dumper.h:124
typedef typeof(((elf_aux_entry *) 0) ->a_un.a_val) elf_aux_val_t
void set_crash_address(uintptr_t crash_address)
Definition: linux_dumper.h:156
const wasteful_vector< elf_aux_val_t > & auxv()
Definition: linux_dumper.h:127
uintptr_t crash_address() const
Definition: linux_dumper.h:155
void set_crash_thread(pid_t crash_thread)
Definition: linux_dumper.h:164
wasteful_vector< elf_aux_val_t > auxv_
Definition: linux_dumper.h:205
void set_crash_signal(int crash_signal)
Definition: linux_dumper.h:161
wasteful_vector< pid_t > threads_
Definition: linux_dumper.h:199