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.cc: Implement google_breakpad::LinuxDumper.
31 // See linux_dumper.h for details.
33 // This code deals with the mechanics of getting information about a crashed
34 // process. Since this code may run in a compromised address space, the same
35 // rules apply as detailed at the top of minidump_writer.h: no libc calls and
36 // use the alternative allocator.
38 #include "client/linux/minidump_writer/linux_dumper.h"
47 #include "client/linux/minidump_writer/line_reader.h"
48 #include "common/linux/elfutils.h"
49 #include "common/linux/file_id.h"
50 #include "common/linux/linux_libc_support.h"
51 #include "common/linux/memory_mapped_file.h"
52 #include "common/linux/safe_readlink.h"
53 #include "third_party/lss/linux_syscall_support.h"
55 static const char kMappedFileUnsafePrefix[] = "/dev/";
56 static const char kDeletedSuffix[] = " (deleted)";
58 inline static bool IsMappedFileOpenUnsafe(
59 const google_breakpad::MappingInfo& mapping) {
60 // It is unsafe to attempt to open a mapped file that lives under /dev,
61 // because the semantics of the open may be driver-specific so we'd risk
62 // hanging the crash dumper. And a file in /dev/ almost certainly has no
63 // ELF file identifier anyways.
64 return my_strncmp(mapping.name,
65 kMappedFileUnsafePrefix,
66 sizeof(kMappedFileUnsafePrefix) - 1) == 0;
69 namespace google_breakpad {
71 // All interesting auvx entry types are below AT_SYSINFO_EHDR
72 #define AT_MAX AT_SYSINFO_EHDR
74 LinuxDumper::LinuxDumper(pid_t pid)
79 threads_(&allocator_, 8),
80 mappings_(&allocator_),
81 auxv_(&allocator_, AT_MAX + 1) {
82 // The passed-in size to the constructor (above) is only a hint.
83 // Must call .resize() to do actual initialization of the elements.
84 auxv_.resize(AT_MAX + 1);
87 LinuxDumper::~LinuxDumper() {
90 bool LinuxDumper::Init() {
91 return ReadAuxv() && EnumerateThreads() && EnumerateMappings();
95 LinuxDumper::ElfFileIdentifierForMapping(const MappingInfo& mapping,
97 unsigned int mapping_id,
98 uint8_t identifier[sizeof(MDGUID)]) {
99 assert(!member || mapping_id < mappings_.size());
100 my_memset(identifier, 0, sizeof(MDGUID));
101 if (IsMappedFileOpenUnsafe(mapping))
104 // Special-case linux-gate because it's not a real file.
105 if (my_strcmp(mapping.name, kLinuxGateLibraryName) == 0) {
106 void* linux_gate = NULL;
107 if (pid_ == sys_getpid()) {
108 linux_gate = reinterpret_cast<void*>(mapping.start_addr);
110 linux_gate = allocator_.Alloc(mapping.size);
111 CopyFromProcess(linux_gate, pid_,
112 reinterpret_cast<const void*>(mapping.start_addr),
115 return FileID::ElfFileIdentifierFromMappedFile(linux_gate, identifier);
118 char filename[NAME_MAX];
119 size_t filename_len = my_strlen(mapping.name);
120 if (filename_len >= NAME_MAX) {
124 my_memcpy(filename, mapping.name, filename_len);
125 filename[filename_len] = '\0';
126 bool filename_modified = HandleDeletedFileInMapping(filename);
128 MemoryMappedFile mapped_file(filename, mapping.offset);
129 if (!mapped_file.data() || mapped_file.size() < SELFMAG)
133 FileID::ElfFileIdentifierFromMappedFile(mapped_file.data(), identifier);
134 if (success && member && filename_modified) {
135 mappings_[mapping_id]->name[filename_len -
136 sizeof(kDeletedSuffix) + 1] = '\0';
143 bool ElfFileSoNameFromMappedFile(
144 const void* elf_base, char* soname, size_t soname_size) {
145 if (!IsValidElf(elf_base)) {
150 const void* segment_start;
153 if (!FindElfSection(elf_base, ".dynamic", SHT_DYNAMIC,
154 &segment_start, &segment_size, &elf_class)) {
155 // No dynamic section
159 const void* dynstr_start;
161 if (!FindElfSection(elf_base, ".dynstr", SHT_STRTAB,
162 &dynstr_start, &dynstr_size, &elf_class)) {
167 const ElfW(Dyn)* dynamic = static_cast<const ElfW(Dyn)*>(segment_start);
168 size_t dcount = segment_size / sizeof(ElfW(Dyn));
169 for (const ElfW(Dyn)* dyn = dynamic; dyn < dynamic + dcount; ++dyn) {
170 if (dyn->d_tag == DT_SONAME) {
171 const char* dynstr = static_cast<const char*>(dynstr_start);
172 if (dyn->d_un.d_val >= dynstr_size) {
173 // Beyond the end of the dynstr section
176 const char* str = dynstr + dyn->d_un.d_val;
177 const size_t maxsize = dynstr_size - dyn->d_un.d_val;
178 my_strlcpy(soname, str, maxsize < soname_size ? maxsize : soname_size);
183 // Did not find SONAME
189 bool LinuxDumper::ElfFileSoName(
190 const MappingInfo& mapping, char* soname, size_t soname_size) {
191 if (IsMappedFileOpenUnsafe(mapping)) {
196 char filename[NAME_MAX];
197 size_t filename_len = my_strlen(mapping.name);
198 if (filename_len >= NAME_MAX) {
204 my_memcpy(filename, mapping.name, filename_len);
205 filename[filename_len] = '\0';
207 MemoryMappedFile mapped_file(filename, mapping.offset);
208 if (!mapped_file.data() || mapped_file.size() < SELFMAG) {
213 return ElfFileSoNameFromMappedFile(mapped_file.data(), soname, soname_size);
216 bool LinuxDumper::ReadAuxv() {
217 char auxv_path[NAME_MAX];
218 if (!BuildProcPath(auxv_path, pid_, "auxv")) {
222 int fd = sys_open(auxv_path, O_RDONLY, 0);
227 elf_aux_entry one_aux_entry;
231 sizeof(elf_aux_entry)) == sizeof(elf_aux_entry) &&
232 one_aux_entry.a_type != AT_NULL) {
233 if (one_aux_entry.a_type <= AT_MAX) {
234 auxv_[one_aux_entry.a_type] = one_aux_entry.a_un.a_val;
242 bool LinuxDumper::EnumerateMappings() {
243 char maps_path[NAME_MAX];
244 if (!BuildProcPath(maps_path, pid_, "maps"))
247 // linux_gate_loc is the beginning of the kernel's mapping of
248 // linux-gate.so in the process. It doesn't actually show up in the
249 // maps list as a filename, but it can be found using the AT_SYSINFO_EHDR
250 // aux vector entry, which gives the information necessary to special
251 // case its entry when creating the list of mappings.
252 // See http://www.trilithium.com/johan/2005/08/linux-gate/ for more
254 const void* linux_gate_loc =
255 reinterpret_cast<void *>(auxv_[AT_SYSINFO_EHDR]);
256 // Although the initial executable is usually the first mapping, it's not
257 // guaranteed (see http://crosbug.com/25355); therefore, try to use the
258 // actual entry point to find the mapping.
259 const void* entry_point_loc = reinterpret_cast<void *>(auxv_[AT_ENTRY]);
261 const int fd = sys_open(maps_path, O_RDONLY, 0);
264 LineReader* const line_reader = new(allocator_) LineReader(fd);
268 while (line_reader->GetNextLine(&line, &line_len)) {
269 uintptr_t start_addr, end_addr, offset;
271 const char* i1 = my_read_hex_ptr(&start_addr, line);
273 const char* i2 = my_read_hex_ptr(&end_addr, i1 + 1);
275 bool exec = (*(i2 + 3) == 'x');
276 const char* i3 = my_read_hex_ptr(&offset, i2 + 6 /* skip ' rwxp ' */);
278 const char* name = NULL;
279 // Only copy name if the name is a valid path name, or if
280 // it's the VDSO image.
281 if (((name = my_strchr(line, '/')) == NULL) &&
283 reinterpret_cast<void*>(start_addr) == linux_gate_loc) {
284 name = kLinuxGateLibraryName;
287 // Merge adjacent mappings with the same name into one module,
288 // assuming they're a single library mapped by the dynamic linker
289 if (name && !mappings_.empty()) {
290 MappingInfo* module = mappings_.back();
291 if ((start_addr == module->start_addr + module->size) &&
292 (my_strlen(name) == my_strlen(module->name)) &&
293 (my_strncmp(name, module->name, my_strlen(name)) == 0)) {
294 module->size = end_addr - module->start_addr;
295 line_reader->PopLine(line_len);
299 MappingInfo* const module = new(allocator_) MappingInfo;
300 my_memset(module, 0, sizeof(MappingInfo));
301 module->start_addr = start_addr;
302 module->size = end_addr - start_addr;
303 module->offset = offset;
306 const unsigned l = my_strlen(name);
307 if (l < sizeof(module->name))
308 my_memcpy(module->name, name, l);
310 // If this is the entry-point mapping, and it's not already the
311 // first one, then we need to make it be first. This is because
312 // the minidump format assumes the first module is the one that
313 // corresponds to the main executable (as codified in
314 // processor/minidump.cc:MinidumpModuleList::GetMainModule()).
315 if (entry_point_loc &&
317 reinterpret_cast<void*>(module->start_addr)) &&
319 reinterpret_cast<void*>(module->start_addr+module->size)) &&
320 !mappings_.empty()) {
321 // push the module onto the front of the list.
322 mappings_.resize(mappings_.size() + 1);
323 for (size_t idx = mappings_.size() - 1; idx > 0; idx--)
324 mappings_[idx] = mappings_[idx - 1];
325 mappings_[0] = module;
327 mappings_.push_back(module);
332 line_reader->PopLine(line_len);
337 return !mappings_.empty();
340 // Get information about the stack, given the stack pointer. We don't try to
341 // walk the stack since we might not have all the information needed to do
342 // unwind. So we just grab, up to, 32k of stack.
343 bool LinuxDumper::GetStackInfo(const void** stack, size_t* stack_len,
344 uintptr_t int_stack_pointer) {
345 // Move the stack pointer to the bottom of the page that it's in.
346 const uintptr_t page_size = getpagesize();
348 uint8_t* const stack_pointer =
349 reinterpret_cast<uint8_t*>(int_stack_pointer & ~(page_size - 1));
351 // The number of bytes of stack which we try to capture.
352 static const ptrdiff_t kStackToCapture = 32 * 1024;
354 const MappingInfo* mapping = FindMapping(stack_pointer);
357 const ptrdiff_t offset = stack_pointer -
358 reinterpret_cast<uint8_t*>(mapping->start_addr);
359 const ptrdiff_t distance_to_end =
360 static_cast<ptrdiff_t>(mapping->size) - offset;
361 *stack_len = distance_to_end > kStackToCapture ?
362 kStackToCapture : distance_to_end;
363 *stack = stack_pointer;
367 // Find the mapping which the given memory address falls in.
368 const MappingInfo* LinuxDumper::FindMapping(const void* address) const {
369 const uintptr_t addr = (uintptr_t) address;
371 for (size_t i = 0; i < mappings_.size(); ++i) {
372 const uintptr_t start = static_cast<uintptr_t>(mappings_[i]->start_addr);
373 if (addr >= start && addr - start < mappings_[i]->size)
380 bool LinuxDumper::HandleDeletedFileInMapping(char* path) const {
381 static const size_t kDeletedSuffixLen = sizeof(kDeletedSuffix) - 1;
383 // Check for ' (deleted)' in |path|.
384 // |path| has to be at least as long as "/x (deleted)".
385 const size_t path_len = my_strlen(path);
386 if (path_len < kDeletedSuffixLen + 2)
388 if (my_strncmp(path + path_len - kDeletedSuffixLen, kDeletedSuffix,
389 kDeletedSuffixLen) != 0) {
393 // Check |path| against the /proc/pid/exe 'symlink'.
394 char exe_link[NAME_MAX];
395 char new_path[NAME_MAX];
396 if (!BuildProcPath(exe_link, pid_, "exe"))
398 if (!SafeReadLink(exe_link, new_path))
400 if (my_strcmp(path, new_path) != 0)
403 // Check to see if someone actually named their executable 'foo (deleted)'.
404 struct kernel_stat exe_stat;
405 struct kernel_stat new_path_stat;
406 if (sys_stat(exe_link, &exe_stat) == 0 &&
407 sys_stat(new_path, &new_path_stat) == 0 &&
408 exe_stat.st_dev == new_path_stat.st_dev &&
409 exe_stat.st_ino == new_path_stat.st_ino) {
413 my_memcpy(path, exe_link, NAME_MAX);
417 } // namespace google_breakpad