blob: 5187120f749ba1f38eeb7ca94e9520d9b4fd934d [file] [log] [blame]
thestig@chromium.org3665a7d2010-11-19 19:57:07 +00001// Copyright (c) 2010, Google Inc.
nealsidb0baafc2009-08-17 23:12:53 +00002// 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// This code deals with the mechanics of getting information about a crashed
31// process. Since this code may run in a compromised address space, the same
32// rules apply as detailed at the top of minidump_writer.h: no libc calls and
33// use the alternative allocator.
34
35#include "client/linux/minidump_writer/linux_dumper.h"
36
ted.mielczarek43378262010-10-21 13:55:07 +000037#include <asm/ptrace.h>
nealsidb0baafc2009-08-17 23:12:53 +000038#include <assert.h>
ted.mielczarek43378262010-10-21 13:55:07 +000039#include <errno.h>
40#include <fcntl.h>
nealsidb0baafc2009-08-17 23:12:53 +000041#include <limits.h>
ted.mielczarek43378262010-10-21 13:55:07 +000042#if !defined(__ANDROID__)
43#include <link.h>
44#endif
nealsidb0baafc2009-08-17 23:12:53 +000045#include <stddef.h>
46#include <stdlib.h>
47#include <stdio.h>
48#include <string.h>
nealsidb0baafc2009-08-17 23:12:53 +000049#include <sys/ptrace.h>
50#include <sys/wait.h>
ted.mielczarek43378262010-10-21 13:55:07 +000051#include <unistd.h>
nealsidb0baafc2009-08-17 23:12:53 +000052
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000053#include <algorithm>
54
nealsidb0baafc2009-08-17 23:12:53 +000055#include "client/linux/minidump_writer/directory_reader.h"
56#include "client/linux/minidump_writer/line_reader.h"
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000057#include "common/linux/file_id.h"
nealsidb0baafc2009-08-17 23:12:53 +000058#include "common/linux/linux_libc_support.h"
thestig@chromium.org0e3b7022010-09-15 22:31:57 +000059#include "third_party/lss/linux_syscall_support.h"
nealsidb0baafc2009-08-17 23:12:53 +000060
nealsid6bc523c2010-05-10 20:35:54 +000061static const char kMappedFileUnsafePrefix[] = "/dev/";
thestig@chromium.org3665a7d2010-11-19 19:57:07 +000062static const char kDeletedSuffix[] = " (deleted)";
nealsid6bc523c2010-05-10 20:35:54 +000063
nealsidb0baafc2009-08-17 23:12:53 +000064// Suspend a thread by attaching to it.
65static bool SuspendThread(pid_t pid) {
66 // This may fail if the thread has just died or debugged.
67 errno = 0;
68 if (sys_ptrace(PTRACE_ATTACH, pid, NULL, NULL) != 0 &&
69 errno != 0) {
70 return false;
71 }
72 while (sys_waitpid(pid, NULL, __WALL) < 0) {
73 if (errno != EINTR) {
74 sys_ptrace(PTRACE_DETACH, pid, NULL, NULL);
75 return false;
76 }
77 }
thestig@chromium.orgf5c8f6f2010-08-14 01:41:39 +000078#if defined(__i386) || defined(__x86_64)
79 // On x86, the stack pointer is NULL or -1, when executing trusted code in
80 // the seccomp sandbox. Not only does this cause difficulties down the line
81 // when trying to dump the thread's stack, it also results in the minidumps
82 // containing information about the trusted threads. This information is
83 // generally completely meaningless and just pollutes the minidumps.
84 // We thus test the stack pointer and exclude any threads that are part of
85 // the seccomp sandbox's trusted code.
86 user_regs_struct regs;
87 if (sys_ptrace(PTRACE_GETREGS, pid, NULL, &regs) == -1 ||
88#if defined(__i386)
89 !regs.esp
90#elif defined(__x86_64)
91 !regs.rsp
92#endif
93 ) {
94 sys_ptrace(PTRACE_DETACH, pid, NULL, NULL);
95 return false;
96 }
97#endif
nealsidb0baafc2009-08-17 23:12:53 +000098 return true;
99}
100
101// Resume a thread by detaching from it.
102static bool ResumeThread(pid_t pid) {
103 return sys_ptrace(PTRACE_DETACH, pid, NULL, NULL) >= 0;
104}
105
nealsid6bc523c2010-05-10 20:35:54 +0000106inline static bool IsMappedFileOpenUnsafe(
ted.mielczarekef7262d2010-12-13 22:10:23 +0000107 const google_breakpad::MappingInfo& mapping) {
nealsid6bc523c2010-05-10 20:35:54 +0000108 // It is unsafe to attempt to open a mapped file that lives under /dev,
109 // because the semantics of the open may be driver-specific so we'd risk
110 // hanging the crash dumper. And a file in /dev/ almost certainly has no
111 // ELF file identifier anyways.
ted.mielczarekef7262d2010-12-13 22:10:23 +0000112 return my_strncmp(mapping.name,
nealsid6bc523c2010-05-10 20:35:54 +0000113 kMappedFileUnsafePrefix,
114 sizeof(kMappedFileUnsafePrefix) - 1) == 0;
115}
116
nealsidb0baafc2009-08-17 23:12:53 +0000117namespace google_breakpad {
118
119LinuxDumper::LinuxDumper(int pid)
120 : pid_(pid),
nealsidde545c02010-03-02 00:39:48 +0000121 threads_suspended_(false),
nealsidb0baafc2009-08-17 23:12:53 +0000122 threads_(&allocator_, 8),
123 mappings_(&allocator_) {
124}
125
126bool LinuxDumper::Init() {
127 return EnumerateThreads(&threads_) &&
128 EnumerateMappings(&mappings_);
129}
130
131bool LinuxDumper::ThreadsSuspend() {
nealsidde545c02010-03-02 00:39:48 +0000132 if (threads_suspended_)
nealsidb0baafc2009-08-17 23:12:53 +0000133 return true;
thestig@chromium.orgf5c8f6f2010-08-14 01:41:39 +0000134 for (size_t i = 0; i < threads_.size(); ++i) {
135 if (!SuspendThread(threads_[i])) {
136 // If the thread either disappeared before we could attach to it, or if
137 // it was part of the seccomp sandbox's trusted code, it is OK to
138 // silently drop it from the minidump.
139 memmove(&threads_[i], &threads_[i+1],
140 (threads_.size() - i - 1) * sizeof(threads_[i]));
141 threads_.resize(threads_.size() - 1);
142 --i;
143 }
144 }
nealsidde545c02010-03-02 00:39:48 +0000145 threads_suspended_ = true;
thestig@chromium.orgf5c8f6f2010-08-14 01:41:39 +0000146 return threads_.size() > 0;
nealsidb0baafc2009-08-17 23:12:53 +0000147}
148
149bool LinuxDumper::ThreadsResume() {
nealsidde545c02010-03-02 00:39:48 +0000150 if (!threads_suspended_)
nealsidb0baafc2009-08-17 23:12:53 +0000151 return false;
152 bool good = true;
153 for (size_t i = 0; i < threads_.size(); ++i)
154 good &= ResumeThread(threads_[i]);
nealsidde545c02010-03-02 00:39:48 +0000155 threads_suspended_ = false;
nealsidb0baafc2009-08-17 23:12:53 +0000156 return good;
157}
158
159void
160LinuxDumper::BuildProcPath(char* path, pid_t pid, const char* node) const {
161 assert(path);
162 if (!path) {
163 return;
164 }
165
166 path[0] = '\0';
167
168 const unsigned pid_len = my_int_len(pid);
169
170 assert(node);
171 if (!node) {
172 return;
173 }
174
175 size_t node_len = my_strlen(node);
176 assert(node_len < NAME_MAX);
177 if (node_len >= NAME_MAX) {
178 return;
179 }
180
181 assert(node_len > 0);
182 if (node_len == 0) {
183 return;
184 }
185
186 assert(pid > 0);
187 if (pid <= 0) {
188 return;
189 }
190
191 const size_t total_length = 6 + pid_len + 1 + node_len;
192
193 assert(total_length < NAME_MAX);
194 if (total_length >= NAME_MAX) {
195 return;
196 }
197
198 memcpy(path, "/proc/", 6);
199 my_itos(path + 6, pid, pid_len);
200 memcpy(path + 6 + pid_len, "/", 1);
201 memcpy(path + 6 + pid_len + 1, node, node_len);
thestig@chromium.org3665a7d2010-11-19 19:57:07 +0000202 path[total_length] = '\0';
nealsidb0baafc2009-08-17 23:12:53 +0000203}
204
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000205bool
ted.mielczarekef7262d2010-12-13 22:10:23 +0000206LinuxDumper::ElfFileIdentifierForMapping(const MappingInfo& mapping,
207 unsigned int mapping_id,
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000208 uint8_t identifier[sizeof(MDGUID)])
209{
ted.mielczarekef7262d2010-12-13 22:10:23 +0000210 assert(mapping_id == -1 || mapping_id < mappings_.size());
nealsid6bc523c2010-05-10 20:35:54 +0000211 my_memset(identifier, 0, sizeof(MDGUID));
thestig@chromium.org3665a7d2010-11-19 19:57:07 +0000212 if (IsMappedFileOpenUnsafe(mapping))
nealsid6bc523c2010-05-10 20:35:54 +0000213 return false;
thestig@chromium.org3665a7d2010-11-19 19:57:07 +0000214
215 char filename[NAME_MAX];
ted.mielczarekef7262d2010-12-13 22:10:23 +0000216 size_t filename_len = my_strlen(mapping.name);
thestig@chromium.org3665a7d2010-11-19 19:57:07 +0000217 assert(filename_len < NAME_MAX);
218 if (filename_len >= NAME_MAX)
219 return false;
ted.mielczarekef7262d2010-12-13 22:10:23 +0000220 memcpy(filename, mapping.name, filename_len);
thestig@chromium.org3665a7d2010-11-19 19:57:07 +0000221 filename[filename_len] = '\0';
222 bool filename_modified = HandleDeletedFileInMapping(filename);
223
224 int fd = sys_open(filename, O_RDONLY, 0);
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000225 if (fd < 0)
226 return false;
227 struct kernel_stat st;
228 if (sys_fstat(fd, &st) != 0) {
229 sys_close(fd);
230 return false;
231 }
ted.mielczarek9f211b42009-12-23 20:44:32 +0000232#if defined(__x86_64)
233#define sys_mmap2 sys_mmap
234#endif
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000235 void* base = sys_mmap2(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
236 sys_close(fd);
237 if (base == MAP_FAILED)
238 return false;
239
240 bool success = FileID::ElfFileIdentifierFromMappedFile(base, identifier);
241 sys_munmap(base, st.st_size);
ted.mielczarekef7262d2010-12-13 22:10:23 +0000242 if (success && mapping_id != -1 && filename_modified) {
243 mappings_[mapping_id]->name[filename_len -
244 sizeof(kDeletedSuffix) + 1] = '\0';
245 }
246
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000247 return success;
248}
249
nealsidb0baafc2009-08-17 23:12:53 +0000250void*
251LinuxDumper::FindBeginningOfLinuxGateSharedLibrary(const pid_t pid) const {
kmixter@chromium.orgb5dfa282010-12-08 22:26:20 +0000252 char auxv_path[NAME_MAX];
nealsidb0baafc2009-08-17 23:12:53 +0000253 BuildProcPath(auxv_path, pid, "auxv");
254
255 // If BuildProcPath errors out due to invalid input, we'll handle it when
256 // we try to sys_open the file.
257
258 // Find the AT_SYSINFO_EHDR entry for linux-gate.so
259 // See http://www.trilithium.com/johan/2005/08/linux-gate/ for more
260 // information.
261 int fd = sys_open(auxv_path, O_RDONLY, 0);
262 if (fd < 0) {
263 return NULL;
264 }
265
266 elf_aux_entry one_aux_entry;
267 while (sys_read(fd,
268 &one_aux_entry,
269 sizeof(elf_aux_entry)) == sizeof(elf_aux_entry) &&
270 one_aux_entry.a_type != AT_NULL) {
271 if (one_aux_entry.a_type == AT_SYSINFO_EHDR) {
272 close(fd);
273 return reinterpret_cast<void*>(one_aux_entry.a_un.a_val);
274 }
275 }
276 close(fd);
277 return NULL;
278}
279
280bool
281LinuxDumper::EnumerateMappings(wasteful_vector<MappingInfo*>* result) const {
kmixter@chromium.orgb5dfa282010-12-08 22:26:20 +0000282 char maps_path[NAME_MAX];
nealsidb0baafc2009-08-17 23:12:53 +0000283 BuildProcPath(maps_path, pid_, "maps");
284
285 // linux_gate_loc is the beginning of the kernel's mapping of
286 // linux-gate.so in the process. It doesn't actually show up in the
287 // maps list as a filename, so we use the aux vector to find it's
288 // load location and special case it's entry when creating the list
289 // of mappings.
290 const void* linux_gate_loc;
291 linux_gate_loc = FindBeginningOfLinuxGateSharedLibrary(pid_);
292
293 const int fd = sys_open(maps_path, O_RDONLY, 0);
294 if (fd < 0)
295 return false;
296 LineReader* const line_reader = new(allocator_) LineReader(fd);
297
298 const char* line;
299 unsigned line_len;
300 while (line_reader->GetNextLine(&line, &line_len)) {
301 uintptr_t start_addr, end_addr, offset;
302
303 const char* i1 = my_read_hex_ptr(&start_addr, line);
304 if (*i1 == '-') {
305 const char* i2 = my_read_hex_ptr(&end_addr, i1 + 1);
306 if (*i2 == ' ') {
307 const char* i3 = my_read_hex_ptr(&offset, i2 + 6 /* skip ' rwxp ' */);
308 if (*i3 == ' ') {
309 MappingInfo* const module = new(allocator_) MappingInfo;
310 memset(module, 0, sizeof(MappingInfo));
311 module->start_addr = start_addr;
312 module->size = end_addr - start_addr;
313 module->offset = offset;
314 const char* name = NULL;
315 // Only copy name if the name is a valid path name, or if
ted.mielczarekef7262d2010-12-13 22:10:23 +0000316 // it's the VDSO image.
nealsidb0baafc2009-08-17 23:12:53 +0000317 if ((name = my_strchr(line, '/')) != NULL) {
318 const unsigned l = my_strlen(name);
319 if (l < sizeof(module->name))
320 memcpy(module->name, name, l);
321 } else if (linux_gate_loc &&
322 reinterpret_cast<void*>(module->start_addr) ==
323 linux_gate_loc) {
324 memcpy(module->name,
325 kLinuxGateLibraryName,
326 my_strlen(kLinuxGateLibraryName));
327 module->offset = 0;
328 }
329 result->push_back(module);
330 }
331 }
332 }
333 line_reader->PopLine(line_len);
334 }
335
336 sys_close(fd);
337
338 return result->size() > 0;
339}
340
341// Parse /proc/$pid/task to list all the threads of the process identified by
342// pid.
343bool LinuxDumper::EnumerateThreads(wasteful_vector<pid_t>* result) const {
kmixter@chromium.orgb5dfa282010-12-08 22:26:20 +0000344 char task_path[NAME_MAX];
nealsidb0baafc2009-08-17 23:12:53 +0000345 BuildProcPath(task_path, pid_, "task");
346
347 const int fd = sys_open(task_path, O_RDONLY | O_DIRECTORY, 0);
348 if (fd < 0)
349 return false;
350 DirectoryReader* dir_reader = new(allocator_) DirectoryReader(fd);
351
352 // The directory may contain duplicate entries which we filter by assuming
353 // that they are consecutive.
354 int last_tid = -1;
355 const char* dent_name;
356 while (dir_reader->GetNextEntry(&dent_name)) {
357 if (my_strcmp(dent_name, ".") &&
358 my_strcmp(dent_name, "..")) {
359 int tid = 0;
360 if (my_strtoui(&tid, dent_name) &&
361 last_tid != tid) {
362 last_tid = tid;
363 result->push_back(tid);
364 }
365 }
366 dir_reader->PopEntry();
367 }
368
369 sys_close(fd);
370 return true;
371}
372
373// Read thread info from /proc/$pid/status.
nealsidde545c02010-03-02 00:39:48 +0000374// Fill out the |tgid|, |ppid| and |pid| members of |info|. If unavailable,
nealsidb0baafc2009-08-17 23:12:53 +0000375// these members are set to -1. Returns true iff all three members are
nealsidde545c02010-03-02 00:39:48 +0000376// available.
nealsidb0baafc2009-08-17 23:12:53 +0000377bool LinuxDumper::ThreadInfoGet(pid_t tid, ThreadInfo* info) {
378 assert(info != NULL);
kmixter@chromium.orgb5dfa282010-12-08 22:26:20 +0000379 char status_path[NAME_MAX];
nealsidb0baafc2009-08-17 23:12:53 +0000380 BuildProcPath(status_path, tid, "status");
381
382 const int fd = open(status_path, O_RDONLY);
383 if (fd < 0)
384 return false;
385
386 LineReader* const line_reader = new(allocator_) LineReader(fd);
387 const char* line;
388 unsigned line_len;
389
390 info->ppid = info->tgid = -1;
391
392 while (line_reader->GetNextLine(&line, &line_len)) {
393 if (my_strncmp("Tgid:\t", line, 6) == 0) {
394 my_strtoui(&info->tgid, line + 6);
395 } else if (my_strncmp("PPid:\t", line, 6) == 0) {
396 my_strtoui(&info->ppid, line + 6);
397 }
398
399 line_reader->PopLine(line_len);
400 }
401
402 if (info->ppid == -1 || info->tgid == -1)
403 return false;
404
ted.mielczarekcfc86282010-10-20 15:51:38 +0000405 if (sys_ptrace(PTRACE_GETREGS, tid, NULL, &info->regs) == -1) {
nealsidb0baafc2009-08-17 23:12:53 +0000406 return false;
407 }
408
ted.mielczarekcfc86282010-10-20 15:51:38 +0000409#if !defined(__ANDROID__)
410 if (sys_ptrace(PTRACE_GETFPREGS, tid, NULL, &info->fpregs) == -1) {
411 return false;
412 }
413#endif
414
nealsid096992f2009-12-01 21:35:52 +0000415#if defined(__i386)
nealsidb0baafc2009-08-17 23:12:53 +0000416 if (sys_ptrace(PTRACE_GETFPXREGS, tid, NULL, &info->fpxregs) == -1)
417 return false;
nealsid096992f2009-12-01 21:35:52 +0000418#endif
nealsidb0baafc2009-08-17 23:12:53 +0000419
nealsid096992f2009-12-01 21:35:52 +0000420#if defined(__i386) || defined(__x86_64)
nealsidb0baafc2009-08-17 23:12:53 +0000421 for (unsigned i = 0; i < ThreadInfo::kNumDebugRegisters; ++i) {
422 if (sys_ptrace(
423 PTRACE_PEEKUSER, tid,
424 reinterpret_cast<void*> (offsetof(struct user,
425 u_debugreg[0]) + i *
426 sizeof(debugreg_t)),
427 &info->dregs[i]) == -1) {
428 return false;
429 }
430 }
431#endif
432
433 const uint8_t* stack_pointer;
434#if defined(__i386)
435 memcpy(&stack_pointer, &info->regs.esp, sizeof(info->regs.esp));
436#elif defined(__x86_64)
437 memcpy(&stack_pointer, &info->regs.rsp, sizeof(info->regs.rsp));
nealsidde545c02010-03-02 00:39:48 +0000438#elif defined(__ARM_EABI__)
ted.mielczarekcfc86282010-10-20 15:51:38 +0000439 memcpy(&stack_pointer, &info->regs.ARM_sp, sizeof(info->regs.ARM_sp));
nealsidb0baafc2009-08-17 23:12:53 +0000440#else
441#error "This code hasn't been ported to your platform yet."
442#endif
443
thestig@chromium.org3665a7d2010-11-19 19:57:07 +0000444 return GetStackInfo(&info->stack, &info->stack_len,
445 (uintptr_t) stack_pointer);
nealsidb0baafc2009-08-17 23:12:53 +0000446}
447
448// Get information about the stack, given the stack pointer. We don't try to
449// walk the stack since we might not have all the information needed to do
450// unwind. So we just grab, up to, 32k of stack.
451bool LinuxDumper::GetStackInfo(const void** stack, size_t* stack_len,
452 uintptr_t int_stack_pointer) {
nealsidb0baafc2009-08-17 23:12:53 +0000453 // Move the stack pointer to the bottom of the page that it's in.
nealsidde545c02010-03-02 00:39:48 +0000454 const uintptr_t page_size = getpagesize();
455
nealsidb0baafc2009-08-17 23:12:53 +0000456 uint8_t* const stack_pointer =
457 reinterpret_cast<uint8_t*>(int_stack_pointer & ~(page_size - 1));
458
459 // The number of bytes of stack which we try to capture.
thestig@chromium.org3665a7d2010-11-19 19:57:07 +0000460 static const ptrdiff_t kStackToCapture = 32 * 1024;
nealsidb0baafc2009-08-17 23:12:53 +0000461
462 const MappingInfo* mapping = FindMapping(stack_pointer);
463 if (!mapping)
464 return false;
nealsidde545c02010-03-02 00:39:48 +0000465 const ptrdiff_t offset = stack_pointer - (uint8_t*) mapping->start_addr;
466 const ptrdiff_t distance_to_end =
467 static_cast<ptrdiff_t>(mapping->size) - offset;
468 *stack_len = distance_to_end > kStackToCapture ?
469 kStackToCapture : distance_to_end;
470 *stack = stack_pointer;
nealsidb0baafc2009-08-17 23:12:53 +0000471 return true;
472}
473
474// static
475void LinuxDumper::CopyFromProcess(void* dest, pid_t child, const void* src,
476 size_t length) {
nealsidde545c02010-03-02 00:39:48 +0000477 unsigned long tmp = 55;
nealsidb0baafc2009-08-17 23:12:53 +0000478 size_t done = 0;
479 static const size_t word_size = sizeof(tmp);
480 uint8_t* const local = (uint8_t*) dest;
481 uint8_t* const remote = (uint8_t*) src;
482
483 while (done < length) {
484 const size_t l = length - done > word_size ? word_size : length - done;
nealsidde545c02010-03-02 00:39:48 +0000485 if (sys_ptrace(PTRACE_PEEKDATA, child, remote + done, &tmp) == -1) {
nealsidb0baafc2009-08-17 23:12:53 +0000486 tmp = 0;
nealsidde545c02010-03-02 00:39:48 +0000487 }
nealsidb0baafc2009-08-17 23:12:53 +0000488 memcpy(local + done, &tmp, l);
489 done += l;
490 }
491}
492
493// Find the mapping which the given memory address falls in.
494const MappingInfo* LinuxDumper::FindMapping(const void* address) const {
495 const uintptr_t addr = (uintptr_t) address;
496
497 for (size_t i = 0; i < mappings_.size(); ++i) {
498 const uintptr_t start = static_cast<uintptr_t>(mappings_[i]->start_addr);
499 if (addr >= start && addr - start < mappings_[i]->size)
500 return mappings_[i];
501 }
502
503 return NULL;
504}
505
ted.mielczarekef7262d2010-12-13 22:10:23 +0000506bool LinuxDumper::HandleDeletedFileInMapping(char* path) const {
thestig@chromium.org3665a7d2010-11-19 19:57:07 +0000507 static const size_t kDeletedSuffixLen = sizeof(kDeletedSuffix) - 1;
508
509 // Check for ' (deleted)' in |path|.
510 // |path| has to be at least as long as "/x (deleted)".
511 const size_t path_len = my_strlen(path);
512 if (path_len < kDeletedSuffixLen + 2)
513 return false;
514 if (my_strncmp(path + path_len - kDeletedSuffixLen, kDeletedSuffix,
515 kDeletedSuffixLen) != 0) {
516 return false;
517 }
518
519 // Check |path| against the /proc/pid/exe 'symlink'.
520 char exe_link[NAME_MAX];
521 char new_path[NAME_MAX];
522 BuildProcPath(exe_link, pid_, "exe");
523 ssize_t new_path_len = sys_readlink(exe_link, new_path, NAME_MAX);
524 if (new_path_len <= 0 || new_path_len == NAME_MAX)
525 return false;
526 new_path[new_path_len] = '\0';
527 if (my_strcmp(path, new_path) != 0)
528 return false;
529
530 // Check to see if someone actually named their executable 'foo (deleted)'.
531 struct kernel_stat exe_stat;
532 struct kernel_stat new_path_stat;
533 if (sys_stat(exe_link, &exe_stat) == 0 &&
534 sys_stat(new_path, &new_path_stat) == 0 &&
535 exe_stat.st_dev == new_path_stat.st_dev &&
536 exe_stat.st_ino == new_path_stat.st_ino) {
537 return false;
538 }
539
540 memcpy(path, exe_link, NAME_MAX);
541 return true;
542}
543
nealsidb0baafc2009-08-17 23:12:53 +0000544} // namespace google_breakpad