1 // Copyright (c) 2009, 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 #ifndef CLIENT_LINUX_MINIDUMP_WRITER_MINIDUMP_WRITER_H_
31 #define CLIENT_LINUX_MINIDUMP_WRITER_MINIDUMP_WRITER_H_
32 
33 #include <stdint.h>
34 #include <sys/types.h>
35 #include <sys/ucontext.h>
36 #include <unistd.h>
37 
38 #include <list>
39 #include <type_traits>
40 #include <utility>
41 
42 #include "linux/minidump_writer/linux_dumper.h"
43 #include "google_breakpad/common/minidump_format.h"
44 
45 namespace google_breakpad {
46 
47 class ExceptionHandler;
48 
49 #if defined(__aarch64__)
50 typedef struct fpsimd_context fpstate_t;
51 #elif !defined(__ARM_EABI__) && !defined(__mips__)
52 typedef std::remove_pointer<fpregset_t>::type fpstate_t;
53 #endif
54 
55 // These entries store a list of memory regions that the client wants included
56 // in the minidump.
57 struct AppMemory {
58   void* ptr;
59   size_t length;
60 
61   bool operator==(const struct AppMemory& other) const {
62     return ptr == other.ptr;
63   }
64 
65   bool operator==(const void* other) const {
66     return ptr == other;
67   }
68 };
69 typedef std::list<AppMemory> AppMemoryList;
70 
71 // Writes a minidump to the filesystem. These functions do not malloc nor use
72 // libc functions which may. Thus, it can be used in contexts where the state
73 // of the heap may be corrupt.
74 //   minidump_path: the path to the file to write to. This is opened O_EXCL and
75 //     fails open fails.
76 //   crashing_process: the pid of the crashing process. This must be trusted.
77 //   blob: a blob of data from the crashing process. See exception_handler.h
78 //   blob_size: the length of |blob|, in bytes
79 //
80 // Returns true iff successful.
81 bool WriteMinidump(const char* minidump_path, pid_t crashing_process,
82                    const void* blob, size_t blob_size,
83                    bool skip_stacks_if_mapping_unreferenced = false,
84                    uintptr_t principal_mapping_address = 0,
85                    bool sanitize_stacks = false);
86 // Same as above but takes an open file descriptor instead of a path.
87 bool WriteMinidump(int minidump_fd, pid_t crashing_process,
88                    const void* blob, size_t blob_size,
89                    bool skip_stacks_if_mapping_unreferenced = false,
90                    uintptr_t principal_mapping_address = 0,
91                    bool sanitize_stacks = false);
92 
93 // Alternate form of WriteMinidump() that works with processes that
94 // are not expected to have crashed.  If |process_blamed_thread| is
95 // meaningful, it will be the one from which a crash signature is
96 // extracted.  It is not expected that this function will be called
97 // from a compromised context, but it is safe to do so.
98 bool WriteMinidump(const char* minidump_path, pid_t process,
99                    pid_t process_blamed_thread);
100 
101 // These overloads also allow passing a list of known mappings and
102 // a list of additional memory regions to be included in the minidump.
103 bool WriteMinidump(const char* minidump_path, pid_t crashing_process,
104                    const void* blob, size_t blob_size,
105                    const MappingList& mappings,
106                    const AppMemoryList& appdata,
107                    bool skip_stacks_if_mapping_unreferenced = false,
108                    uintptr_t principal_mapping_address = 0,
109                    bool sanitize_stacks = false);
110 bool WriteMinidump(int minidump_fd, pid_t crashing_process,
111                    const void* blob, size_t blob_size,
112                    const MappingList& mappings,
113                    const AppMemoryList& appdata,
114                    bool skip_stacks_if_mapping_unreferenced = false,
115                    uintptr_t principal_mapping_address = 0,
116                    bool sanitize_stacks = false);
117 
118 // These overloads also allow passing a file size limit for the minidump.
119 bool WriteMinidump(const char* minidump_path, off_t minidump_size_limit,
120                    pid_t crashing_process,
121                    const void* blob, size_t blob_size,
122                    const MappingList& mappings,
123                    const AppMemoryList& appdata,
124                    bool skip_stacks_if_mapping_unreferenced = false,
125                    uintptr_t principal_mapping_address = 0,
126                    bool sanitize_stacks = false);
127 bool WriteMinidump(int minidump_fd, off_t minidump_size_limit,
128                    pid_t crashing_process,
129                    const void* blob, size_t blob_size,
130                    const MappingList& mappings,
131                    const AppMemoryList& appdata,
132                    bool skip_stacks_if_mapping_unreferenced = false,
133                    uintptr_t principal_mapping_address = 0,
134                    bool sanitize_stacks = false);
135 
136 bool WriteMinidump(const char* filename,
137                    const MappingList& mappings,
138                    const AppMemoryList& appdata,
139                    LinuxDumper* dumper);
140 
141 }  // namespace google_breakpad
142 
143 #endif  // CLIENT_LINUX_MINIDUMP_WRITER_MINIDUMP_WRITER_H_
144