xref: /aosp_15_r20/external/google-breakpad/src/client/linux/minidump_writer/minidump_writer.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2009 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifndef CLIENT_LINUX_MINIDUMP_WRITER_MINIDUMP_WRITER_H_
30 #define CLIENT_LINUX_MINIDUMP_WRITER_MINIDUMP_WRITER_H_
31 
32 #include <stdint.h>
33 #include <sys/types.h>
34 #include <sys/ucontext.h>
35 #include <unistd.h>
36 
37 #include <list>
38 #include <type_traits>
39 #include <utility>
40 
41 #include "client/linux/minidump_writer/linux_dumper.h"
42 #include "google_breakpad/common/minidump_format.h"
43 
44 namespace google_breakpad {
45 
46 class ExceptionHandler;
47 
48 #if defined(__aarch64__)
49 typedef struct fpsimd_context fpstate_t;
50 #elif !defined(__ARM_EABI__) && !defined(__mips__)
51 typedef std::remove_pointer<fpregset_t>::type fpstate_t;
52 #endif
53 
54 // These entries store a list of memory regions that the client wants included
55 // in the minidump.
56 struct AppMemory {
57   void* ptr;
58   size_t length;
59 
60   bool operator==(const struct AppMemory& other) const {
61     return ptr == other.ptr;
62   }
63 
64   bool operator==(const void* other) const {
65     return ptr == other;
66   }
67 };
68 typedef std::list<AppMemory> AppMemoryList;
69 
70 // Writes a minidump to the filesystem. These functions do not malloc nor use
71 // libc functions which may. Thus, it can be used in contexts where the state
72 // of the heap may be corrupt.
73 //   minidump_path: the path to the file to write to. This is opened O_EXCL and
74 //     fails open fails.
75 //   crashing_process: the pid of the crashing process. This must be trusted.
76 //   blob: a blob of data from the crashing process. See exception_handler.h
77 //   blob_size: the length of |blob|, in bytes
78 //
79 // Returns true iff successful.
80 bool WriteMinidump(const char* minidump_path, pid_t crashing_process,
81                    const void* blob, size_t blob_size,
82                    bool skip_stacks_if_mapping_unreferenced = false,
83                    uintptr_t principal_mapping_address = 0,
84                    bool sanitize_stacks = false);
85 // Same as above but takes an open file descriptor instead of a path.
86 bool WriteMinidump(int minidump_fd, pid_t crashing_process,
87                    const void* blob, size_t blob_size,
88                    bool skip_stacks_if_mapping_unreferenced = false,
89                    uintptr_t principal_mapping_address = 0,
90                    bool sanitize_stacks = false);
91 
92 // Alternate form of WriteMinidump() that works with processes that
93 // are not expected to have crashed.  If |process_blamed_thread| is
94 // meaningful, it will be the one from which a crash signature is
95 // extracted.  It is not expected that this function will be called
96 // from a compromised context, but it is safe to do so.
97 bool WriteMinidump(const char* minidump_path, pid_t process,
98                    pid_t process_blamed_thread);
99 
100 // These overloads also allow passing a list of known mappings and
101 // a list of additional memory regions to be included in the minidump.
102 bool WriteMinidump(const char* minidump_path, pid_t crashing_process,
103                    const void* blob, size_t blob_size,
104                    const MappingList& mappings,
105                    const AppMemoryList& appdata,
106                    bool skip_stacks_if_mapping_unreferenced = false,
107                    uintptr_t principal_mapping_address = 0,
108                    bool sanitize_stacks = false);
109 bool WriteMinidump(int minidump_fd, pid_t crashing_process,
110                    const void* blob, size_t blob_size,
111                    const MappingList& mappings,
112                    const AppMemoryList& appdata,
113                    bool skip_stacks_if_mapping_unreferenced = false,
114                    uintptr_t principal_mapping_address = 0,
115                    bool sanitize_stacks = false);
116 
117 // These overloads also allow passing a file size limit for the minidump.
118 bool WriteMinidump(const char* minidump_path, off_t minidump_size_limit,
119                    pid_t crashing_process,
120                    const void* blob, size_t blob_size,
121                    const MappingList& mappings,
122                    const AppMemoryList& appdata,
123                    bool skip_stacks_if_mapping_unreferenced = false,
124                    uintptr_t principal_mapping_address = 0,
125                    bool sanitize_stacks = false);
126 bool WriteMinidump(int minidump_fd, off_t minidump_size_limit,
127                    pid_t crashing_process,
128                    const void* blob, size_t blob_size,
129                    const MappingList& mappings,
130                    const AppMemoryList& appdata,
131                    bool skip_stacks_if_mapping_unreferenced = false,
132                    uintptr_t principal_mapping_address = 0,
133                    bool sanitize_stacks = false);
134 
135 bool WriteMinidump(const char* filename,
136                    const MappingList& mappings,
137                    const AppMemoryList& appdata,
138                    LinuxDumper* dumper);
139 
140 }  // namespace google_breakpad
141 
142 #endif  // CLIENT_LINUX_MINIDUMP_WRITER_MINIDUMP_WRITER_H_
143