1// Copyright (c) 2012, Google Inc.
2// 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// linux_core_dumper.h: Define the google_breakpad::LinuxCoreDumper
31// class, which is derived from google_breakpad::LinuxDumper to extract
32// information from a crashed process via its core dump and proc files.
33
34#ifndef CLIENT_LINUX_MINIDUMP_WRITER_LINUX_CORE_DUMPER_H_
35#define CLIENT_LINUX_MINIDUMP_WRITER_LINUX_CORE_DUMPER_H_
36
37#include "client/linux/minidump_writer/linux_dumper.h"
38#include "common/linux/elf_core_dump.h"
39#include "common/linux/memory_mapped_file.h"
40
41namespace google_breakpad {
42
43class LinuxCoreDumper : public LinuxDumper {
44 public:
45  // Constructs a dumper for extracting information of a given process
46  // with a process ID of |pid| via its core dump file at |core_path| and
47  // its proc files at |procfs_path|. If |procfs_path| is a copy of
48  // /proc/<pid>, it should contain the following files:
49  //     auxv, cmdline, environ, exe, maps, status
50  LinuxCoreDumper(pid_t pid, const char* core_path, const char* procfs_path);
51
52  // Implements LinuxDumper::BuildProcPath().
53  // Builds a proc path for a certain pid for a node (/proc/<pid>/<node>).
54  // |path| is a character array of at least NAME_MAX bytes to return the
55  // result.|node| is the final node without any slashes. Return true on
56  // success.
57  //
58  // As this dumper performs a post-mortem dump and makes use of a copy
59  // of the proc files of the crashed process, this derived method does
60  // not actually make use of |pid| and always returns a subpath of
61  // |procfs_path_| regardless of whether |pid| corresponds to the main
62  // process or a thread of the process, i.e. assuming both the main process
63  // and its threads have the following proc files with the same content:
64  //     auxv, cmdline, environ, exe, maps, status
65  virtual bool BuildProcPath(char* path, pid_t pid, const char* node) const;
66
67  // Implements LinuxDumper::CopyFromProcess().
68  // Copies content of |length| bytes from a given process |child|,
69  // starting from |src|, into |dest|. This method extracts the content
70  // the core dump and fills |dest| with a sequence of marker bytes
71  // if the expected data is not found in the core dump. Returns true if
72  // the expected data is found in the core dump.
73  virtual bool CopyFromProcess(void* dest, pid_t child, const void* src,
74                               size_t length);
75
76  // Implements LinuxDumper::GetThreadInfoByIndex().
77  // Reads information about the |index|-th thread of |threads_|.
78  // Returns true on success. One must have called |ThreadsSuspend| first.
79  virtual bool GetThreadInfoByIndex(size_t index, ThreadInfo* info);
80
81  // Implements LinuxDumper::IsPostMortem().
82  // Always returns true to indicate that this dumper performs a
83  // post-mortem dump of a crashed process via a core dump file.
84  virtual bool IsPostMortem() const;
85
86  // Implements LinuxDumper::ThreadsSuspend().
87  // As the dumper performs a post-mortem dump via a core dump file,
88  // there is no threads to suspend. This method does nothing and
89  // always returns true.
90  virtual bool ThreadsSuspend();
91
92  // Implements LinuxDumper::ThreadsResume().
93  // As the dumper performs a post-mortem dump via a core dump file,
94  // there is no threads to resume. This method does nothing and
95  // always returns true.
96  virtual bool ThreadsResume();
97
98 protected:
99  // Implements LinuxDumper::EnumerateThreads().
100  // Enumerates all threads of the given process into |threads_|.
101  virtual bool EnumerateThreads();
102
103 private:
104  // Path of the core dump file.
105  const char* core_path_;
106
107  // Path of the directory containing the proc files of the given process,
108  // which is usually a copy of /proc/<pid>.
109  const char* procfs_path_;
110
111  // Memory-mapped core dump file at |core_path_|.
112  MemoryMappedFile mapped_core_file_;
113
114  // Content of the core dump file.
115  ElfCoreDump core_;
116
117  // Thread info found in the core dump file.
118  wasteful_vector<ThreadInfo> thread_infos_;
119};
120
121}  // namespace google_breakpad
122
123#endif  // CLIENT_LINUX_HANDLER_LINUX_CORE_DUMPER_H_
124