13665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org// Copyright (c) 2010, Google Inc.
2b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// All rights reserved.
3b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid//
4b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// Redistribution and use in source and binary forms, with or without
5b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// modification, are permitted provided that the following conditions are
6b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// met:
7b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid//
8b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid//     * Redistributions of source code must retain the above copyright
9b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// notice, this list of conditions and the following disclaimer.
10b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid//     * Redistributions in binary form must reproduce the above
11b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// copyright notice, this list of conditions and the following disclaimer
12b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// in the documentation and/or other materials provided with the
13b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// distribution.
14b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid//     * Neither the name of Google Inc. nor the names of its
15b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// contributors may be used to endorse or promote products derived from
16b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// this software without specific prior written permission.
17b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid//
18b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
304fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org// linux_dumper.h: Define the google_breakpad::LinuxDumper class, which
314fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org// is a base class for extracting information of a crashed process. It
324fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org// was originally a complete implementation using the ptrace API, but
334fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org// has been refactored to allow derived implementations supporting both
344fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org// ptrace and core dump. A portion of the original implementation is now
354fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org// in google_breakpad::LinuxPtraceDumper (see linux_ptrace_dumper.h for
364fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org// details).
374fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org
38b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid#ifndef CLIENT_LINUX_MINIDUMP_WRITER_LINUX_DUMPER_H_
39b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid#define CLIENT_LINUX_MINIDUMP_WRITER_LINUX_DUMPER_H_
40b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
41b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid#include <elf.h>
42096992fac73756cfa0974a94754329f30fd4e786nealsid#include <linux/limits.h>
43b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid#include <stdint.h>
44096992fac73756cfa0974a94754329f30fd4e786nealsid#include <sys/types.h>
45b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid#include <sys/user.h>
46b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
4730f1b7fb16cdc906078f031bf12c1ffb2cb5ea3eprimiano@chromium.org#include "client/linux/dump_writer_common/mapping_info.h"
4830f1b7fb16cdc906078f031bf12c1ffb2cb5ea3eprimiano@chromium.org#include "client/linux/dump_writer_common/thread_info.h"
494621ee06914b2ebe963c93ea78fabf982cf670dfted.mielczarek#include "common/memory.h"
500a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek#include "google_breakpad/common/minidump_format.h"
51b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
52b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidnamespace google_breakpad {
53b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
54b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// Typedef for our parsing of the auxv variables in /proc/pid/auxv.
555f22d6a7f471f2352d394c188560fd06830e14f3gordana.cmiljanovic@imgtec.com#if defined(__i386) || defined(__ARM_EABI__) || defined(__mips__)
56b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidtypedef Elf32_auxv_t elf_aux_entry;
572eedc625759a8943d72711769a84fce05a23ecd0rmcilroy@chromium.org#elif defined(__x86_64) || defined(__aarch64__)
58b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidtypedef Elf64_auxv_t elf_aux_entry;
59b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid#endif
60693d4caa50791a40a8b9734054603dde2b05e0fbted.mielczarek@gmail.com
6120335445a30a0fe9941eb946c82fc7a945d85a24ted.mielczarek@gmail.comtypedef __typeof__(((elf_aux_entry*) 0)->a_un.a_val) elf_aux_val_t;
62693d4caa50791a40a8b9734054603dde2b05e0fbted.mielczarek@gmail.com
63b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// When we find the VDSO mapping in the process's address space, this
64b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// is the name we use for it when writing it to the minidump.
65b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid// This should always be less than NAME_MAX!
66b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidconst char kLinuxGateLibraryName[] = "linux-gate.so";
67b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
68b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidclass LinuxDumper {
69b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid public:
70b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  explicit LinuxDumper(pid_t pid);
71b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
72e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  virtual ~LinuxDumper();
73e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org
74b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  // Parse the data for |threads| and |mappings|.
754fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  virtual bool Init();
764fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org
774fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  // Return true if the dumper performs a post-mortem dump.
784fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  virtual bool IsPostMortem() const = 0;
79b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
80b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  // Suspend/resume all threads in the given process.
814fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  virtual bool ThreadsSuspend() = 0;
824fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  virtual bool ThreadsResume() = 0;
83b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
84e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  // Read information about the |index|-th thread of |threads_|.
85e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  // Returns true on success. One must have called |ThreadsSuspend| first.
864fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  virtual bool GetThreadInfoByIndex(size_t index, ThreadInfo* info) = 0;
87b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
88b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  // These are only valid after a call to |Init|.
89b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  const wasteful_vector<pid_t> &threads() { return threads_; }
90b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  const wasteful_vector<MappingInfo*> &mappings() { return mappings_; }
91b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  const MappingInfo* FindMapping(const void* address) const;
92693d4caa50791a40a8b9734054603dde2b05e0fbted.mielczarek@gmail.com  const wasteful_vector<elf_aux_val_t>& auxv() { return auxv_; }
93b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
94b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  // Find a block of memory to take as the stack given the top of stack pointer.
95b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  //   stack: (output) the lowest address in the memory area
96b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  //   stack_len: (output) the length of the memory area
97b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  //   stack_top: the current top of the stack
98b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  bool GetStackInfo(const void** stack, size_t* stack_len, uintptr_t stack_top);
99b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
100b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  PageAllocator* allocator() { return &allocator_; }
101b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
102e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  // Copy content of |length| bytes from a given process |child|,
103e0aa94bfb6682a04f824b64d064fbb58ac5343c7benchan@chromium.org  // starting from |src|, into |dest|. Returns true on success.
104e0aa94bfb6682a04f824b64d064fbb58ac5343c7benchan@chromium.org  virtual bool CopyFromProcess(void* dest, pid_t child, const void* src,
1054fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org                               size_t length) = 0;
106b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
1074fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  // Builds a proc path for a certain pid for a node (/proc/<pid>/<node>).
1084fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  // |path| is a character array of at least NAME_MAX bytes to return the
1094fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  // result.|node| is the final node without any slashes. Returns true on
1104fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  // success.
1114fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  virtual bool BuildProcPath(char* path, pid_t pid, const char* node) const = 0;
112b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
113ef7262d4775bf6de750bc2a26dbf98368d7ec0c3ted.mielczarek  // Generate a File ID from the .text section of a mapped entry.
11484c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org  // If not a member, mapping_id is ignored. This method can also manipulate the
11584c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org  // |mapping|.name to truncate "(deleted)" from the file name if necessary.
116ef7262d4775bf6de750bc2a26dbf98368d7ec0c3ted.mielczarek  bool ElfFileIdentifierForMapping(const MappingInfo& mapping,
11723c82999a8a8a6761133a7a8e88b5349bcff211ajessicag.feedback@gmail.com                                   bool member,
11823c82999a8a8a6761133a7a8e88b5349bcff211ajessicag.feedback@gmail.com                                   unsigned int mapping_id,
1190a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek                                   uint8_t identifier[sizeof(MDGUID)]);
1200a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek
121e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  uintptr_t crash_address() const { return crash_address_; }
122e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  void set_crash_address(uintptr_t crash_address) {
123e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org    crash_address_ = crash_address;
124e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  }
125e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org
126e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  int crash_signal() const { return crash_signal_; }
127e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  void set_crash_signal(int crash_signal) { crash_signal_ = crash_signal; }
128e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org
129e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  pid_t crash_thread() const { return crash_thread_; }
130e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  void set_crash_thread(pid_t crash_thread) { crash_thread_ = crash_thread; }
131e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org
13284c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org  // Extracts the effective path and file name of from |mapping|. In most cases
13384c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org  // the effective name/path are just the mapping's path and basename. In some
13484c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org  // other cases, however, a library can be mapped from an archive (e.g., when
13584c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org  // loading .so libs from an apk on Android) and this method is able to
13684c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org  // reconstruct the original file name.
13784c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org  static void GetMappingEffectiveNameAndPath(const MappingInfo& mapping,
13884c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org                                             char* file_path,
13984c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org                                             size_t file_path_size,
14084c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org                                             char* file_name,
14184c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org                                             size_t file_name_size);
14284c92677474d91cdc1585a9d663eae30272b807aprimiano@chromium.org
1434fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org protected:
144693d4caa50791a40a8b9734054603dde2b05e0fbted.mielczarek@gmail.com  bool ReadAuxv();
145693d4caa50791a40a8b9734054603dde2b05e0fbted.mielczarek@gmail.com
1464fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  virtual bool EnumerateMappings();
1474fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org
1484fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  virtual bool EnumerateThreads() = 0;
149b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
1503665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org  // For the case where a running program has been deleted, it'll show up in
1513665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org  // /proc/pid/maps as "/path/to/program (deleted)". If this is the case, then
1523665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org  // see if '/path/to/program (deleted)' matches /proc/pid/exe and return
1533665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org  // /proc/pid/exe in |path| so ELF identifier generation works correctly. This
1543665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org  // also checks to see if '/path/to/program (deleted)' exists, so it does not
1553665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org  // get fooled by a poorly named binary.
1563665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org  // For programs that don't end with ' (deleted)', this is a no-op.
1573665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org  // This assumes |path| is a buffer with length NAME_MAX.
1583665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org  // Returns true if |path| is modified.
159ef7262d4775bf6de750bc2a26dbf98368d7ec0c3ted.mielczarek  bool HandleDeletedFileInMapping(char* path) const;
1603665a7d09bb32ab3939635daec398618dd1ef955thestig@chromium.org
161e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org   // ID of the crashed process.
162b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  const pid_t pid_;
163b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
164e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  // Virtual address at which the process crashed.
165e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  uintptr_t crash_address_;
166e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org
167e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  // Signal that terminated the crashed process.
168e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  int crash_signal_;
169e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org
170e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  // ID of the crashed thread.
171e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org  pid_t crash_thread_;
172e8bbceddb1bef18462c3504d10c60a7936a2f530benchan@chromium.org
173b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  mutable PageAllocator allocator_;
174b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
1754fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  // IDs of all the threads.
1764fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  wasteful_vector<pid_t> threads_;
1774fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org
1784fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  // Info from /proc/<pid>/maps.
1794fa638a7ecb6ab042664300767614308dbc147bbbenchan@chromium.org  wasteful_vector<MappingInfo*> mappings_;
180693d4caa50791a40a8b9734054603dde2b05e0fbted.mielczarek@gmail.com
181693d4caa50791a40a8b9734054603dde2b05e0fbted.mielczarek@gmail.com  // Info from /proc/<pid>/auxv
182693d4caa50791a40a8b9734054603dde2b05e0fbted.mielczarek@gmail.com  wasteful_vector<elf_aux_val_t> auxv_;
183b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid};
184b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
185b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid}  // namespace google_breakpad
186b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
187b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid#endif  // CLIENT_LINUX_HANDLER_LINUX_DUMPER_H_
188