1 // Copyright (c) 2010, 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_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
38 #ifndef CLIENT_LINUX_MINIDUMP_WRITER_LINUX_DUMPER_H_
39 #define CLIENT_LINUX_MINIDUMP_WRITER_LINUX_DUMPER_H_
43 #if defined(__ANDROID__)
46 #include <linux/limits.h>
48 #include <sys/types.h>
53 #include "linux/dump_writer_common/mapping_info.h"
54 #include "linux/dump_writer_common/thread_info.h"
55 #include "common/linux/file_id.h"
56 #include "common/memory_allocator.h"
57 #include "google_breakpad/common/minidump_format.h"
60 # include "linux_utils.h"
61 #endif // defined(XP_LINUX)
63 namespace google_breakpad
{
65 // Typedef for our parsing of the auxv variables in /proc/pid/auxv.
66 #if defined(__i386) || defined(__ARM_EABI__) || \
67 (defined(__mips__) && _MIPS_SIM == _ABIO32)
68 typedef Elf32_auxv_t elf_aux_entry
;
69 #elif defined(__x86_64) || defined(__aarch64__) || \
70 (defined(__mips__) && _MIPS_SIM != _ABIO32)
71 typedef Elf64_auxv_t elf_aux_entry
;
74 typedef __typeof__(((elf_aux_entry
*) 0)->a_un
.a_val
) elf_aux_val_t
;
76 // When we find the VDSO mapping in the process's address space, this
77 // is the name we use for it when writing it to the minidump.
78 // This should always be less than NAME_MAX!
79 const char kLinuxGateLibraryName
[] = "linux-gate.so";
83 // The |root_prefix| is prepended to mapping paths before opening them, which
84 // is useful if the crash originates from a chroot.
85 explicit LinuxDumper(pid_t pid
, const char* root_prefix
= "");
87 virtual ~LinuxDumper();
89 // Parse the data for |threads| and |mappings|.
92 // Take any actions that could not be taken in Init(). LateInit() is
93 // called after all other caller's initialization is complete, and in
94 // particular after it has called ThreadsSuspend(), so that ptrace is
96 virtual bool LateInit();
98 // Return true if the dumper performs a post-mortem dump.
99 virtual bool IsPostMortem() const = 0;
101 // Suspend/resume all threads in the given process.
102 virtual bool ThreadsSuspend() = 0;
103 virtual bool ThreadsResume() = 0;
105 // Read information about the |index|-th thread of |threads_|.
106 // Returns true on success. One must have called |ThreadsSuspend| first.
107 virtual bool GetThreadInfoByIndex(size_t index
, ThreadInfo
* info
) = 0;
109 // Read the name ofthe |index|-th thread of |threads_|.
110 // Returns true on success. One must have called |ThreadsSuspend| first.
111 virtual bool GetThreadNameByIndex(size_t index
, char* name
, size_t size
) = 0;
113 size_t GetMainThreadIndex() const {
114 for (size_t i
= 0; i
< threads_
.size(); ++i
) {
115 if (threads_
[i
] == pid_
) return i
;
120 // These are only valid after a call to |Init|.
121 const wasteful_vector
<pid_t
> &threads() { return threads_
; }
122 const wasteful_vector
<MappingInfo
*> &mappings() { return mappings_
; }
123 const MappingInfo
* FindMapping(const void* address
) const;
124 // Find the mapping which the given memory address falls in. Unlike
125 // FindMapping, this method uses the unadjusted mapping address
126 // ranges from the kernel, rather than the ranges that have had the
127 // load bias applied.
128 const MappingInfo
* FindMappingNoBias(uintptr_t address
) const;
129 const wasteful_vector
<elf_aux_val_t
>& auxv() { return auxv_
; }
131 // Find a block of memory to take as the stack given the top of stack pointer.
132 // stack: (output) the lowest address in the memory area
133 // stack_len: (output) the length of the memory area
134 // stack_top: the current top of the stack
135 bool GetStackInfo(const void** stack
, size_t* stack_len
, uintptr_t stack_top
);
137 // Sanitize a copy of the stack by overwriting words that are not
138 // pointers with a sentinel (0x0defaced).
139 // stack_copy: a copy of the stack to sanitize. |stack_copy| might
140 // not be word aligned, but it represents word aligned
141 // data copied from another location.
142 // stack_len: the length of the allocation pointed to by |stack_copy|.
143 // stack_pointer: the address of the stack pointer (used to locate
144 // the stack mapping, as an optimization).
145 // sp_offset: the offset relative to stack_copy that reflects the
146 // current value of the stack pointer.
147 void SanitizeStackCopy(uint8_t* stack_copy
, size_t stack_len
,
148 uintptr_t stack_pointer
, uintptr_t sp_offset
);
150 // Test whether |stack_copy| contains a pointer-aligned word that
151 // could be an address within a given mapping.
152 // stack_copy: a copy of the stack to check. |stack_copy| might
153 // not be word aligned, but it represents word aligned
154 // data copied from another location.
155 // stack_len: the length of the allocation pointed to by |stack_copy|.
156 // sp_offset: the offset relative to stack_copy that reflects the
157 // current value of the stack pointer.
158 // mapping: the mapping against which to test stack words.
159 bool StackHasPointerToMapping(const uint8_t* stack_copy
, size_t stack_len
,
161 const MappingInfo
& mapping
);
163 PageAllocator
* allocator() { return &allocator_
; }
165 // Copy content of |length| bytes from a given process |child|,
166 // starting from |src|, into |dest|. Returns true on success.
167 virtual bool CopyFromProcess(void* dest
, pid_t child
, const void* src
,
170 // Builds a proc path for a certain pid for a node (/proc/<pid>/<node>).
171 // |path| is a character array of at least NAME_MAX bytes to return the
172 // result.|node| is the final node without any slashes. Returns true on
174 virtual bool BuildProcPath(char* path
, pid_t pid
, const char* node
) const = 0;
176 // Generate a File ID from the .text section of a mapped entry.
177 // If not a member, mapping_id is ignored. This method can also manipulate the
178 // |mapping|.name to truncate "(deleted)" from the file name if necessary.
179 bool ElfFileIdentifierForMapping(const MappingInfo
& mapping
,
181 unsigned int mapping_id
,
182 wasteful_vector
<uint8_t>& identifier
);
184 void SetCrashInfoFromSigInfo(const siginfo_t
& siginfo
);
186 uintptr_t crash_address() const { return crash_address_
; }
187 void set_crash_address(uintptr_t crash_address
) {
188 crash_address_
= crash_address
;
191 int crash_signal() const { return crash_signal_
; }
192 void set_crash_signal(int crash_signal
) { crash_signal_
= crash_signal
; }
193 const char* GetCrashSignalString() const;
195 void set_crash_signal_code(int code
) { crash_signal_code_
= code
; }
196 int crash_signal_code() const { return crash_signal_code_
; }
198 void set_crash_exception_info(const std::vector
<uint64_t>& exception_info
) {
199 assert(exception_info
.size() <= MD_EXCEPTION_MAXIMUM_PARAMETERS
);
200 crash_exception_info_
= exception_info
;
202 const std::vector
<uint64_t>& crash_exception_info() const {
203 return crash_exception_info_
;
206 pid_t
pid() const { return pid_
; }
207 pid_t
crash_thread() const { return crash_thread_
; }
208 void set_crash_thread(pid_t crash_thread
) { crash_thread_
= crash_thread
; }
210 // Concatenates the |root_prefix_| and |mapping| path. Writes into |path| and
211 // returns true unless the string is too long.
212 bool GetMappingAbsolutePath(const MappingInfo
& mapping
,
213 char path
[PATH_MAX
]) const;
215 // Extracts the effective path and file name of from |mapping|. In most cases
216 // the effective name/path are just the mapping's path and basename. In some
217 // other cases, however, a library can be mapped from an archive (e.g., when
218 // loading .so libs from an apk on Android) and this method is able to
219 // reconstruct the original file name.
220 void GetMappingEffectiveNamePathAndVersion(const MappingInfo
& mapping
,
222 size_t file_path_size
,
224 size_t file_name_size
,
225 VersionComponents
* version
);
230 virtual bool EnumerateMappings();
232 virtual bool EnumerateThreads() = 0;
234 // For the case where a running program has been deleted, it'll show up in
235 // /proc/pid/maps as "/path/to/program (deleted)". If this is the case, then
236 // see if '/path/to/program (deleted)' matches /proc/pid/exe and return
237 // /proc/pid/exe in |path| so ELF identifier generation works correctly. This
238 // also checks to see if '/path/to/program (deleted)' exists, so it does not
239 // get fooled by a poorly named binary.
240 // For programs that don't end with ' (deleted)', this is a no-op.
241 // This assumes |path| is a buffer with length NAME_MAX.
242 // Returns true if |path| is modified.
243 bool HandleDeletedFileInMapping(char* path
) const;
245 // ID of the crashed process.
248 // Path of the root directory to which mapping paths are relative.
249 const char* const root_prefix_
;
251 // Virtual address at which the process crashed.
252 uintptr_t crash_address_
;
254 // Signal that terminated the crashed process.
257 // The code associated with |crash_signal_|.
258 int crash_signal_code_
;
260 // The additional fields associated with |crash_signal_|.
261 std::vector
<uint64_t> crash_exception_info_
;
263 // ID of the crashed thread.
266 mutable PageAllocator allocator_
;
268 // IDs of all the threads.
269 wasteful_vector
<pid_t
> threads_
;
271 // Info from /proc/<pid>/maps.
272 wasteful_vector
<MappingInfo
*> mappings_
;
274 // Info from /proc/<pid>/auxv
275 wasteful_vector
<elf_aux_val_t
> auxv_
;
278 bool IsIPCSharedMemorySegment(const char* name
);
280 #if defined(__ANDROID__)
281 // Android M and later support packed ELF relocations in shared libraries.
282 // Packing relocations changes the vaddr of the LOAD segments, such that
283 // the effective load bias is no longer the same as the start address of
284 // the memory mapping containing the executable parts of the library. The
285 // packing is applied to the stripped library run on the target, but not to
286 // any other library, and in particular not to the library used to generate
287 // breakpad symbols. As a result, we need to adjust the |start_addr| for
288 // any mapping that results from a shared library that contains Android
289 // packed relocations, so that it properly represents the effective library
290 // load bias. The following functions support this adjustment.
292 // Check that a given mapping at |start_addr| is for an ELF shared library.
293 // If it is, place the ELF header in |ehdr| and return true.
294 // The first LOAD segment in an ELF shared library has offset zero, so the
295 // ELF file header is at the start of this map entry, and in already mapped
297 bool GetLoadedElfHeader(uintptr_t start_addr
, ElfW(Ehdr
)* ehdr
);
299 // For the ELF file mapped at |start_addr|, iterate ELF program headers to
300 // find the min vaddr of all program header LOAD segments, the vaddr for
301 // the DYNAMIC segment, and a count of DYNAMIC entries. Return values in
302 // |min_vaddr_ptr|, |dyn_vaddr_ptr|, and |dyn_count_ptr|.
303 // The program header table is also in already mapped memory.
304 void ParseLoadedElfProgramHeaders(ElfW(Ehdr
)* ehdr
,
305 uintptr_t start_addr
,
306 uintptr_t* min_vaddr_ptr
,
307 uintptr_t* dyn_vaddr_ptr
,
308 size_t* dyn_count_ptr
);
310 // Search the DYNAMIC tags for the ELF file with the given |load_bias|, and
311 // return true if the tags indicate that the file contains Android packed
312 // relocations. Dynamic tags are found at |dyn_vaddr| past the |load_bias|.
313 bool HasAndroidPackedRelocations(uintptr_t load_bias
,
317 // If the ELF file mapped at |start_addr| contained Android packed
318 // relocations, return the load bias that the system linker (or Chromium
319 // crazy linker) will have used. If the file did not contain Android
320 // packed relocations, returns |start_addr|, indicating that no adjustment
322 // The effective load bias is |start_addr| adjusted downwards by the
323 // min vaddr in the library LOAD segments.
324 uintptr_t GetEffectiveLoadBias(ElfW(Ehdr
)* ehdr
, uintptr_t start_addr
);
326 // Called from LateInit(). Iterates |mappings_| and rewrites the |start_addr|
327 // field of any that represent ELF shared libraries with Android packed
328 // relocations, so that |start_addr| is the load bias that the system linker
329 // (or Chromium crazy linker) used. This value matches the addresses produced
330 // when the non-relocation-packed library is used for breakpad symbol
332 void LatePostprocessMappings();
333 #endif // __ANDROID__
336 } // namespace google_breakpad
338 #endif // CLIENT_LINUX_HANDLER_LINUX_DUMPER_H_