1 : // 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 : #ifndef CLIENT_LINUX_MINIDUMP_WRITER_LINUX_DUMPER_H_
31 : #define CLIENT_LINUX_MINIDUMP_WRITER_LINUX_DUMPER_H_
32 :
33 : #include <elf.h>
34 : #include <linux/limits.h>
35 : #include <stdint.h>
36 : #include <sys/types.h>
37 : #if !defined(__ANDROID__)
38 : #include <sys/user.h>
39 : #endif
40 :
41 : #include "common/memory.h"
42 : #include "google_breakpad/common/minidump_format.h"
43 : #include <asm/ptrace.h>
44 :
45 : namespace google_breakpad {
46 :
47 : #if defined(__i386) || defined(__x86_64)
48 : typedef typeof(((struct user*) 0)->u_debugreg[0]) debugreg_t;
49 : #endif
50 :
51 : // Typedef for our parsing of the auxv variables in /proc/pid/auxv.
52 : #if defined(__i386) || defined(__ARM_EABI__)
53 : #if !defined(__ANDROID__)
54 : typedef Elf32_auxv_t elf_aux_entry;
55 : #else
56 : // Android is missing this structure definition
57 : typedef struct
58 : {
59 : uint32_t a_type; /* Entry type */
60 : union
61 : {
62 : uint32_t a_val; /* Integer value */
63 : } a_un;
64 : } elf_aux_entry;
65 :
66 : #if !defined(AT_SYSINFO_EHDR)
67 : #define AT_SYSINFO_EHDR 33
68 : #endif
69 : #endif // __ANDROID__
70 : #elif defined(__x86_64__)
71 : typedef Elf64_auxv_t elf_aux_entry;
72 : #endif
73 : // When we find the VDSO mapping in the process's address space, this
74 : // is the name we use for it when writing it to the minidump.
75 : // This should always be less than NAME_MAX!
76 : const char kLinuxGateLibraryName[] = "linux-gate.so";
77 :
78 : // We produce one of these structures for each thread in the crashed process.
79 : struct ThreadInfo {
80 : pid_t tid; // thread id
81 : pid_t tgid; // thread group id
82 : pid_t ppid; // parent process
83 :
84 : // Even on platforms where the stack grows down, the following will point to
85 : // the smallest address in the stack.
86 : const void* stack; // pointer to the stack area
87 : size_t stack_len; // length of the stack to copy
88 :
89 :
90 : #if defined(__i386) || defined(__x86_64)
91 : user_regs_struct regs;
92 : user_fpregs_struct fpregs;
93 : static const unsigned kNumDebugRegisters = 8;
94 : debugreg_t dregs[8];
95 : #if defined(__i386)
96 : user_fpxregs_struct fpxregs;
97 : #endif // defined(__i386)
98 :
99 : #elif defined(__ARM_EABI__)
100 : // Mimicking how strace does this(see syscall.c, search for GETREGS)
101 : #if defined(__ANDROID__)
102 : struct pt_regs regs;
103 : #else
104 : struct user_regs regs;
105 : struct user_fpregs fpregs;
106 : #endif // __ANDROID__
107 : #endif
108 : };
109 :
110 : // One of these is produced for each mapping in the process (i.e. line in
111 : // /proc/$x/maps).
112 : struct MappingInfo {
113 : uintptr_t start_addr;
114 : size_t size;
115 : size_t offset; // offset into the backed file.
116 : char name[NAME_MAX];
117 : };
118 :
119 : // Suspend a thread by attaching to it.
120 : bool AttachThread(pid_t pid);
121 :
122 : // Resume a thread by detaching from it.
123 : bool DetachThread(pid_t pid);
124 :
125 : // Fill |info| with the register state of |info->tid|. The thread
126 : // must be attached to the calling process. Return true on success.
127 : bool GetThreadRegisters(ThreadInfo* info);
128 :
129 0 : class LinuxDumper {
130 : public:
131 : explicit LinuxDumper(pid_t pid);
132 :
133 : // Parse the data for |threads| and |mappings|.
134 : bool Init();
135 :
136 : // Attach/detach all threads in the given process.
137 : bool ThreadsAttach();
138 : bool ThreadsDetach();
139 :
140 : // Read information about the given thread. Returns true on success. One must
141 : // have called |ThreadsAttach| first.
142 : bool ThreadInfoGet(ThreadInfo* info);
143 :
144 : // These are only valid after a call to |Init|.
145 0 : const wasteful_vector<pid_t> &threads() { return threads_; }
146 0 : const wasteful_vector<MappingInfo*> &mappings() { return mappings_; }
147 : const MappingInfo* FindMapping(const void* address) const;
148 :
149 : // Find a block of memory to take as the stack given the top of stack pointer.
150 : // stack: (output) the lowest address in the memory area
151 : // stack_len: (output) the length of the memory area
152 : // stack_top: the current top of the stack
153 : bool GetStackInfo(const void** stack, size_t* stack_len, uintptr_t stack_top);
154 :
155 0 : PageAllocator* allocator() { return &allocator_; }
156 :
157 : // memcpy from a remote process.
158 : static void CopyFromProcess(void* dest, pid_t child, const void* src,
159 : size_t length);
160 :
161 : // Builds a proc path for a certain pid for a node. path is a
162 : // character array that is overwritten, and node is the final node
163 : // without any slashes.
164 : void BuildProcPath(char* path, pid_t pid, const char* node) const;
165 :
166 : // Generate a File ID from the .text section of a mapped entry
167 : bool ElfFileIdentifierForMapping(const MappingInfo& mapping,
168 : uint8_t identifier[sizeof(MDGUID)]);
169 :
170 : // Utility method to find the location of where the kernel has
171 : // mapped linux-gate.so in memory(shows up in /proc/pid/maps as
172 : // [vdso], but we can't guarantee that it's the only virtual dynamic
173 : // shared object. Parsing the auxilary vector for AT_SYSINFO_EHDR
174 : // is the safest way to go.)
175 : void* FindBeginningOfLinuxGateSharedLibrary(const pid_t pid) const;
176 : private:
177 : bool EnumerateMappings(wasteful_vector<MappingInfo*>* result) const;
178 : bool EnumerateThreads(wasteful_vector<pid_t>* result) const;
179 :
180 : const pid_t pid_;
181 :
182 : mutable PageAllocator allocator_;
183 :
184 : bool threads_suspended_;
185 : wasteful_vector<pid_t> threads_; // the ids of all the threads
186 : wasteful_vector<MappingInfo*> mappings_; // info from /proc/<pid>/maps
187 : };
188 :
189 : } // namespace google_breakpad
190 :
191 : #endif // CLIENT_LINUX_HANDLER_LINUX_DUMPER_H_
|