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"
46
#include "client/linux/minidump_writer/line_reader.h"
47
#include "common/linux/file_id.h"
48
#include "common/linux/linux_libc_support.h"
49
#include "common/linux/memory_mapped_file.h"
50
#include "common/linux/safe_readlink.h"
51
#include "third_party/lss/linux_syscall_support.h"
53
static const char kMappedFileUnsafePrefix[] = "/dev/";
54
static const char kDeletedSuffix[] = " (deleted)";
56
inline static bool IsMappedFileOpenUnsafe(
57
const google_breakpad::MappingInfo& mapping) {
58
// It is unsafe to attempt to open a mapped file that lives under /dev,
59
// because the semantics of the open may be driver-specific so we'd risk
60
// hanging the crash dumper. And a file in /dev/ almost certainly has no
61
// ELF file identifier anyways.
62
return my_strncmp(mapping.name,
63
kMappedFileUnsafePrefix,
64
sizeof(kMappedFileUnsafePrefix) - 1) == 0;
67
namespace google_breakpad {
69
LinuxDumper::LinuxDumper(pid_t pid)
74
threads_(&allocator_, 8),
75
mappings_(&allocator_) {
78
LinuxDumper::~LinuxDumper() {
81
bool LinuxDumper::Init() {
82
return EnumerateThreads() && EnumerateMappings();
86
LinuxDumper::ElfFileIdentifierForMapping(const MappingInfo& mapping,
88
unsigned int mapping_id,
89
uint8_t identifier[sizeof(MDGUID)])
91
assert(!member || mapping_id < mappings_.size());
92
my_memset(identifier, 0, sizeof(MDGUID));
93
if (IsMappedFileOpenUnsafe(mapping))
96
// Special-case linux-gate because it's not a real file.
97
if (my_strcmp(mapping.name, kLinuxGateLibraryName) == 0) {
98
const uintptr_t kPageSize = getpagesize();
99
void* linux_gate = NULL;
100
if (pid_ == sys_getpid()) {
101
linux_gate = reinterpret_cast<void*>(mapping.start_addr);
103
linux_gate = allocator_.Alloc(kPageSize);
104
CopyFromProcess(linux_gate, pid_,
105
reinterpret_cast<const void*>(mapping.start_addr),
108
return FileID::ElfFileIdentifierFromMappedFile(linux_gate, identifier);
111
char filename[NAME_MAX];
112
size_t filename_len = my_strlen(mapping.name);
113
assert(filename_len < NAME_MAX);
114
if (filename_len >= NAME_MAX)
116
memcpy(filename, mapping.name, filename_len);
117
filename[filename_len] = '\0';
118
bool filename_modified = HandleDeletedFileInMapping(filename);
120
MemoryMappedFile mapped_file(filename);
121
if (!mapped_file.data()) // Should probably check if size >= ElfW(Ehdr)?
125
FileID::ElfFileIdentifierFromMappedFile(mapped_file.data(), identifier);
126
if (success && member && filename_modified) {
127
mappings_[mapping_id]->name[filename_len -
128
sizeof(kDeletedSuffix) + 1] = '\0';
135
LinuxDumper::FindBeginningOfLinuxGateSharedLibrary(pid_t pid) const {
136
char auxv_path[NAME_MAX];
137
if (!BuildProcPath(auxv_path, pid, "auxv"))
140
// Find the AT_SYSINFO_EHDR entry for linux-gate.so
141
// See http://www.trilithium.com/johan/2005/08/linux-gate/ for more
143
int fd = sys_open(auxv_path, O_RDONLY, 0);
148
elf_aux_entry one_aux_entry;
151
sizeof(elf_aux_entry)) == sizeof(elf_aux_entry) &&
152
one_aux_entry.a_type != AT_NULL) {
153
if (one_aux_entry.a_type == AT_SYSINFO_EHDR) {
155
return reinterpret_cast<void*>(one_aux_entry.a_un.a_val);
163
LinuxDumper::FindEntryPoint(pid_t pid) const {
164
char auxv_path[NAME_MAX];
165
if (!BuildProcPath(auxv_path, pid, "auxv"))
168
int fd = sys_open(auxv_path, O_RDONLY, 0);
173
// Find the AT_ENTRY entry
174
elf_aux_entry one_aux_entry;
177
sizeof(elf_aux_entry)) == sizeof(elf_aux_entry) &&
178
one_aux_entry.a_type != AT_NULL) {
179
if (one_aux_entry.a_type == AT_ENTRY) {
181
return reinterpret_cast<void*>(one_aux_entry.a_un.a_val);
188
bool LinuxDumper::EnumerateMappings() {
189
char maps_path[NAME_MAX];
190
if (!BuildProcPath(maps_path, pid_, "maps"))
193
// linux_gate_loc is the beginning of the kernel's mapping of
194
// linux-gate.so in the process. It doesn't actually show up in the
195
// maps list as a filename, so we use the aux vector to find it's
196
// load location and special case it's entry when creating the list
198
const void* linux_gate_loc;
199
linux_gate_loc = FindBeginningOfLinuxGateSharedLibrary(pid_);
200
// Although the initial executable is usually the first mapping, it's not
201
// guaranteed (see http://crosbug.com/25355); therefore, try to use the
202
// actual entry point to find the mapping.
203
const void* entry_point_loc = FindEntryPoint(pid_);
205
const int fd = sys_open(maps_path, O_RDONLY, 0);
208
LineReader* const line_reader = new(allocator_) LineReader(fd);
212
while (line_reader->GetNextLine(&line, &line_len)) {
213
uintptr_t start_addr, end_addr, offset;
215
const char* i1 = my_read_hex_ptr(&start_addr, line);
217
const char* i2 = my_read_hex_ptr(&end_addr, i1 + 1);
219
const char* i3 = my_read_hex_ptr(&offset, i2 + 6 /* skip ' rwxp ' */);
221
const char* name = NULL;
222
// Only copy name if the name is a valid path name, or if
223
// it's the VDSO image.
224
if (((name = my_strchr(line, '/')) == NULL) &&
226
reinterpret_cast<void*>(start_addr) == linux_gate_loc) {
227
name = kLinuxGateLibraryName;
230
// Merge adjacent mappings with the same name into one module,
231
// assuming they're a single library mapped by the dynamic linker
232
if (name && !mappings_.empty()) {
233
MappingInfo* module = mappings_.back();
234
if ((start_addr == module->start_addr + module->size) &&
235
(my_strlen(name) == my_strlen(module->name)) &&
236
(my_strncmp(name, module->name, my_strlen(name)) == 0)) {
237
module->size = end_addr - module->start_addr;
238
line_reader->PopLine(line_len);
242
MappingInfo* const module = new(allocator_) MappingInfo;
243
memset(module, 0, sizeof(MappingInfo));
244
module->start_addr = start_addr;
245
module->size = end_addr - start_addr;
246
module->offset = offset;
248
const unsigned l = my_strlen(name);
249
if (l < sizeof(module->name))
250
memcpy(module->name, name, l);
252
// If this is the entry-point mapping, and it's not already the
253
// first one, then we need to make it be first. This is because
254
// the minidump format assumes the first module is the one that
255
// corresponds to the main executable (as codified in
256
// processor/minidump.cc:MinidumpModuleList::GetMainModule()).
257
if (entry_point_loc &&
259
reinterpret_cast<void*>(module->start_addr)) &&
261
reinterpret_cast<void*>(module->start_addr+module->size)) &&
262
!mappings_.empty()) {
263
// push the module onto the front of the list.
264
mappings_.resize(mappings_.size() + 1);
265
for (size_t idx = mappings_.size() - 1; idx > 0; idx--)
266
mappings_[idx] = mappings_[idx - 1];
267
mappings_[0] = module;
269
mappings_.push_back(module);
274
line_reader->PopLine(line_len);
279
return !mappings_.empty();
282
// Get information about the stack, given the stack pointer. We don't try to
283
// walk the stack since we might not have all the information needed to do
284
// unwind. So we just grab, up to, 32k of stack.
285
bool LinuxDumper::GetStackInfo(const void** stack, size_t* stack_len,
286
uintptr_t int_stack_pointer) {
287
// Move the stack pointer to the bottom of the page that it's in.
288
const uintptr_t page_size = getpagesize();
290
uint8_t* const stack_pointer =
291
reinterpret_cast<uint8_t*>(int_stack_pointer & ~(page_size - 1));
293
// The number of bytes of stack which we try to capture.
294
static const ptrdiff_t kStackToCapture = 32 * 1024;
296
const MappingInfo* mapping = FindMapping(stack_pointer);
299
const ptrdiff_t offset = stack_pointer - (uint8_t*) mapping->start_addr;
300
const ptrdiff_t distance_to_end =
301
static_cast<ptrdiff_t>(mapping->size) - offset;
302
*stack_len = distance_to_end > kStackToCapture ?
303
kStackToCapture : distance_to_end;
304
*stack = stack_pointer;
308
// Find the mapping which the given memory address falls in.
309
const MappingInfo* LinuxDumper::FindMapping(const void* address) const {
310
const uintptr_t addr = (uintptr_t) address;
312
for (size_t i = 0; i < mappings_.size(); ++i) {
313
const uintptr_t start = static_cast<uintptr_t>(mappings_[i]->start_addr);
314
if (addr >= start && addr - start < mappings_[i]->size)
321
bool LinuxDumper::HandleDeletedFileInMapping(char* path) const {
322
static const size_t kDeletedSuffixLen = sizeof(kDeletedSuffix) - 1;
324
// Check for ' (deleted)' in |path|.
325
// |path| has to be at least as long as "/x (deleted)".
326
const size_t path_len = my_strlen(path);
327
if (path_len < kDeletedSuffixLen + 2)
329
if (my_strncmp(path + path_len - kDeletedSuffixLen, kDeletedSuffix,
330
kDeletedSuffixLen) != 0) {
334
// Check |path| against the /proc/pid/exe 'symlink'.
335
char exe_link[NAME_MAX];
336
char new_path[NAME_MAX];
337
if (!BuildProcPath(exe_link, pid_, "exe"))
339
if (!SafeReadLink(exe_link, new_path))
341
if (my_strcmp(path, new_path) != 0)
344
// Check to see if someone actually named their executable 'foo (deleted)'.
345
struct kernel_stat exe_stat;
346
struct kernel_stat new_path_stat;
347
if (sys_stat(exe_link, &exe_stat) == 0 &&
348
sys_stat(new_path, &new_path_stat) == 0 &&
349
exe_stat.st_dev == new_path_stat.st_dev &&
350
exe_stat.st_ino == new_path_stat.st_ino) {
354
memcpy(path, exe_link, NAME_MAX);
358
} // namespace google_breakpad