Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // minidump_file_writer.h: Implements file-based minidump generation. It's |
michael@0 | 31 | // intended to be used with the Google Breakpad open source crash handling |
michael@0 | 32 | // project. |
michael@0 | 33 | |
michael@0 | 34 | #ifndef CLIENT_MINIDUMP_FILE_WRITER_H__ |
michael@0 | 35 | #define CLIENT_MINIDUMP_FILE_WRITER_H__ |
michael@0 | 36 | |
michael@0 | 37 | #include <string> |
michael@0 | 38 | |
michael@0 | 39 | #include "google_breakpad/common/minidump_format.h" |
michael@0 | 40 | |
michael@0 | 41 | namespace google_breakpad { |
michael@0 | 42 | |
michael@0 | 43 | class UntypedMDRVA; |
michael@0 | 44 | template<typename MDType> class TypedMDRVA; |
michael@0 | 45 | |
michael@0 | 46 | // The user of this class can Open() a file and add minidump streams, data, and |
michael@0 | 47 | // strings using the definitions in minidump_format.h. Since this class is |
michael@0 | 48 | // expected to be used in a situation where the current process may be |
michael@0 | 49 | // damaged, it will not allocate heap memory. |
michael@0 | 50 | // Sample usage: |
michael@0 | 51 | // MinidumpFileWriter writer; |
michael@0 | 52 | // writer.Open("/tmp/minidump.dmp"); |
michael@0 | 53 | // TypedMDRVA<MDRawHeader> header(&writer_); |
michael@0 | 54 | // header.Allocate(); |
michael@0 | 55 | // header->get()->signature = MD_HEADER_SIGNATURE; |
michael@0 | 56 | // : |
michael@0 | 57 | // writer.Close(); |
michael@0 | 58 | // |
michael@0 | 59 | // An alternative is to use SetFile and provide a file descriptor: |
michael@0 | 60 | // MinidumpFileWriter writer; |
michael@0 | 61 | // writer.SetFile(minidump_fd); |
michael@0 | 62 | // TypedMDRVA<MDRawHeader> header(&writer_); |
michael@0 | 63 | // header.Allocate(); |
michael@0 | 64 | // header->get()->signature = MD_HEADER_SIGNATURE; |
michael@0 | 65 | // : |
michael@0 | 66 | // writer.Close(); |
michael@0 | 67 | |
michael@0 | 68 | class MinidumpFileWriter { |
michael@0 | 69 | public: |
michael@0 | 70 | // Invalid MDRVA (Minidump Relative Virtual Address) |
michael@0 | 71 | // returned on failed allocation |
michael@0 | 72 | static const MDRVA kInvalidMDRVA; |
michael@0 | 73 | |
michael@0 | 74 | MinidumpFileWriter(); |
michael@0 | 75 | ~MinidumpFileWriter(); |
michael@0 | 76 | |
michael@0 | 77 | // Open |path| as the destination of the minidump data. Any existing file |
michael@0 | 78 | // will be overwritten. |
michael@0 | 79 | // Return true on success, or false on failure. |
michael@0 | 80 | bool Open(const char *path); |
michael@0 | 81 | |
michael@0 | 82 | // Sets the file descriptor |file| as the destination of the minidump data. |
michael@0 | 83 | // Can be used as an alternative to Open() when a file descriptor is |
michael@0 | 84 | // available. |
michael@0 | 85 | // Note that |fd| is not closed when the instance of MinidumpFileWriter is |
michael@0 | 86 | // destroyed. |
michael@0 | 87 | void SetFile(const int file); |
michael@0 | 88 | |
michael@0 | 89 | // Close the current file (that was either created when Open was called, or |
michael@0 | 90 | // specified with SetFile). |
michael@0 | 91 | // Return true on success, or false on failure. |
michael@0 | 92 | bool Close(); |
michael@0 | 93 | |
michael@0 | 94 | // Copy the contents of |str| to a MDString and write it to the file. |
michael@0 | 95 | // |str| is expected to be either UTF-16 or UTF-32 depending on the size |
michael@0 | 96 | // of wchar_t. |
michael@0 | 97 | // Maximum |length| of characters to copy from |str|, or specify 0 to use the |
michael@0 | 98 | // entire NULL terminated string. Copying will stop at the first NULL. |
michael@0 | 99 | // |location| the allocated location |
michael@0 | 100 | // Return true on success, or false on failure |
michael@0 | 101 | bool WriteString(const wchar_t *str, unsigned int length, |
michael@0 | 102 | MDLocationDescriptor *location); |
michael@0 | 103 | |
michael@0 | 104 | // Same as above, except with |str| as a UTF-8 string |
michael@0 | 105 | bool WriteString(const char *str, unsigned int length, |
michael@0 | 106 | MDLocationDescriptor *location); |
michael@0 | 107 | |
michael@0 | 108 | // Write |size| bytes starting at |src| into the current position. |
michael@0 | 109 | // Return true on success and set |output| to position, or false on failure |
michael@0 | 110 | bool WriteMemory(const void *src, size_t size, MDMemoryDescriptor *output); |
michael@0 | 111 | |
michael@0 | 112 | // Copies |size| bytes from |src| to |position| |
michael@0 | 113 | // Return true on success, or false on failure |
michael@0 | 114 | bool Copy(MDRVA position, const void *src, ssize_t size); |
michael@0 | 115 | |
michael@0 | 116 | // Return the current position for writing to the minidump |
michael@0 | 117 | inline MDRVA position() const { return position_; } |
michael@0 | 118 | |
michael@0 | 119 | private: |
michael@0 | 120 | friend class UntypedMDRVA; |
michael@0 | 121 | |
michael@0 | 122 | // Allocates an area of |size| bytes. |
michael@0 | 123 | // Returns the position of the allocation, or kInvalidMDRVA if it was |
michael@0 | 124 | // unable to allocate the bytes. |
michael@0 | 125 | MDRVA Allocate(size_t size); |
michael@0 | 126 | |
michael@0 | 127 | // The file descriptor for the output file. |
michael@0 | 128 | int file_; |
michael@0 | 129 | |
michael@0 | 130 | // Whether |file_| should be closed when the instance is destroyed. |
michael@0 | 131 | bool close_file_when_destroyed_; |
michael@0 | 132 | |
michael@0 | 133 | // Current position in buffer |
michael@0 | 134 | MDRVA position_; |
michael@0 | 135 | |
michael@0 | 136 | // Current allocated size |
michael@0 | 137 | size_t size_; |
michael@0 | 138 | |
michael@0 | 139 | // Copy |length| characters from |str| to |mdstring|. These are distinct |
michael@0 | 140 | // because the underlying MDString is a UTF-16 based string. The wchar_t |
michael@0 | 141 | // variant may need to create a MDString that has more characters than the |
michael@0 | 142 | // source |str|, whereas the UTF-8 variant may coalesce characters to form |
michael@0 | 143 | // a single UTF-16 character. |
michael@0 | 144 | bool CopyStringToMDString(const wchar_t *str, unsigned int length, |
michael@0 | 145 | TypedMDRVA<MDString> *mdstring); |
michael@0 | 146 | bool CopyStringToMDString(const char *str, unsigned int length, |
michael@0 | 147 | TypedMDRVA<MDString> *mdstring); |
michael@0 | 148 | |
michael@0 | 149 | // The common templated code for writing a string |
michael@0 | 150 | template <typename CharType> |
michael@0 | 151 | bool WriteStringCore(const CharType *str, unsigned int length, |
michael@0 | 152 | MDLocationDescriptor *location); |
michael@0 | 153 | }; |
michael@0 | 154 | |
michael@0 | 155 | // Represents an untyped allocated chunk |
michael@0 | 156 | class UntypedMDRVA { |
michael@0 | 157 | public: |
michael@0 | 158 | explicit UntypedMDRVA(MinidumpFileWriter *writer) |
michael@0 | 159 | : writer_(writer), |
michael@0 | 160 | position_(writer->position()), |
michael@0 | 161 | size_(0) {} |
michael@0 | 162 | |
michael@0 | 163 | // Allocates |size| bytes. Must not call more than once. |
michael@0 | 164 | // Return true on success, or false on failure |
michael@0 | 165 | bool Allocate(size_t size); |
michael@0 | 166 | |
michael@0 | 167 | // Returns the current position or kInvalidMDRVA if allocation failed |
michael@0 | 168 | inline MDRVA position() const { return position_; } |
michael@0 | 169 | |
michael@0 | 170 | // Number of bytes allocated |
michael@0 | 171 | inline size_t size() const { return size_; } |
michael@0 | 172 | |
michael@0 | 173 | // Return size and position |
michael@0 | 174 | inline MDLocationDescriptor location() const { |
michael@0 | 175 | MDLocationDescriptor location = { static_cast<uint32_t>(size_), |
michael@0 | 176 | position_ }; |
michael@0 | 177 | return location; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | // Copy |size| bytes starting at |src| into the minidump at |position| |
michael@0 | 181 | // Return true on success, or false on failure |
michael@0 | 182 | bool Copy(MDRVA position, const void *src, size_t size); |
michael@0 | 183 | |
michael@0 | 184 | // Copy |size| bytes from |src| to the current position |
michael@0 | 185 | inline bool Copy(const void *src, size_t size) { |
michael@0 | 186 | return Copy(position_, src, size); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | protected: |
michael@0 | 190 | // Writer we associate with |
michael@0 | 191 | MinidumpFileWriter *writer_; |
michael@0 | 192 | |
michael@0 | 193 | // Position of the start of the data |
michael@0 | 194 | MDRVA position_; |
michael@0 | 195 | |
michael@0 | 196 | // Allocated size |
michael@0 | 197 | size_t size_; |
michael@0 | 198 | }; |
michael@0 | 199 | |
michael@0 | 200 | // Represents a Minidump object chunk. Additional memory can be allocated at |
michael@0 | 201 | // the end of the object as a: |
michael@0 | 202 | // - single allocation |
michael@0 | 203 | // - Array of MDType objects |
michael@0 | 204 | // - A MDType object followed by an array |
michael@0 | 205 | template<typename MDType> |
michael@0 | 206 | class TypedMDRVA : public UntypedMDRVA { |
michael@0 | 207 | public: |
michael@0 | 208 | // Constructs an unallocated MDRVA |
michael@0 | 209 | explicit TypedMDRVA(MinidumpFileWriter *writer) |
michael@0 | 210 | : UntypedMDRVA(writer), |
michael@0 | 211 | data_(), |
michael@0 | 212 | allocation_state_(UNALLOCATED) {} |
michael@0 | 213 | |
michael@0 | 214 | inline ~TypedMDRVA() { |
michael@0 | 215 | // Ensure that the data_ object is written out |
michael@0 | 216 | if (allocation_state_ != ARRAY) |
michael@0 | 217 | Flush(); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | // Address of object data_ of MDType. This is not declared const as the |
michael@0 | 221 | // typical usage will be to access the underlying |data_| object as to |
michael@0 | 222 | // alter its contents. |
michael@0 | 223 | MDType *get() { return &data_; } |
michael@0 | 224 | |
michael@0 | 225 | // Allocates minidump_size<MDType>::size() bytes. |
michael@0 | 226 | // Must not call more than once. |
michael@0 | 227 | // Return true on success, or false on failure |
michael@0 | 228 | bool Allocate(); |
michael@0 | 229 | |
michael@0 | 230 | // Allocates minidump_size<MDType>::size() + |additional| bytes. |
michael@0 | 231 | // Must not call more than once. |
michael@0 | 232 | // Return true on success, or false on failure |
michael@0 | 233 | bool Allocate(size_t additional); |
michael@0 | 234 | |
michael@0 | 235 | // Allocate an array of |count| elements of MDType. |
michael@0 | 236 | // Must not call more than once. |
michael@0 | 237 | // Return true on success, or false on failure |
michael@0 | 238 | bool AllocateArray(size_t count); |
michael@0 | 239 | |
michael@0 | 240 | // Allocate an array of |count| elements of |size| after object of MDType |
michael@0 | 241 | // Must not call more than once. |
michael@0 | 242 | // Return true on success, or false on failure |
michael@0 | 243 | bool AllocateObjectAndArray(size_t count, size_t size); |
michael@0 | 244 | |
michael@0 | 245 | // Copy |item| to |index| |
michael@0 | 246 | // Must have been allocated using AllocateArray(). |
michael@0 | 247 | // Return true on success, or false on failure |
michael@0 | 248 | bool CopyIndex(unsigned int index, MDType *item); |
michael@0 | 249 | |
michael@0 | 250 | // Copy |size| bytes starting at |str| to |index| |
michael@0 | 251 | // Must have been allocated using AllocateObjectAndArray(). |
michael@0 | 252 | // Return true on success, or false on failure |
michael@0 | 253 | bool CopyIndexAfterObject(unsigned int index, const void *src, size_t size); |
michael@0 | 254 | |
michael@0 | 255 | // Write data_ |
michael@0 | 256 | bool Flush(); |
michael@0 | 257 | |
michael@0 | 258 | private: |
michael@0 | 259 | enum AllocationState { |
michael@0 | 260 | UNALLOCATED = 0, |
michael@0 | 261 | SINGLE_OBJECT, |
michael@0 | 262 | ARRAY, |
michael@0 | 263 | SINGLE_OBJECT_WITH_ARRAY |
michael@0 | 264 | }; |
michael@0 | 265 | |
michael@0 | 266 | MDType data_; |
michael@0 | 267 | AllocationState allocation_state_; |
michael@0 | 268 | }; |
michael@0 | 269 | |
michael@0 | 270 | } // namespace google_breakpad |
michael@0 | 271 | |
michael@0 | 272 | #endif // CLIENT_MINIDUMP_FILE_WRITER_H__ |