| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006, 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 // minidump_file_writer.cc: Minidump file writer implementation. | |
| 31 // | |
| 32 // See minidump_file_writer.h for documentation. | |
| 33 | |
| 34 #include <fcntl.h> | |
| 35 #include <limits.h> | |
| 36 #include <stdio.h> | |
| 37 #include <string.h> | |
| 38 #include <unistd.h> | |
| 39 | |
| 40 #include "breakpad/linux/linux_syscall_support.h" | |
| 41 #include "breakpad/linux/linux_libc_support.h" | |
| 42 #include "client/minidump_file_writer-inl.h" | |
| 43 #include "common/string_conversion.h" | |
| 44 | |
| 45 namespace google_breakpad { | |
| 46 | |
| 47 const MDRVA MinidumpFileWriter::kInvalidMDRVA = static_cast<MDRVA>(-1); | |
| 48 | |
| 49 MinidumpFileWriter::MinidumpFileWriter() : file_(-1), position_(0), size_(0) { | |
| 50 } | |
| 51 | |
| 52 MinidumpFileWriter::~MinidumpFileWriter() { | |
| 53 Close(); | |
| 54 } | |
| 55 | |
| 56 bool MinidumpFileWriter::Open(const char *path) { | |
| 57 assert(file_ == -1); | |
| 58 file_ = sys_open(path, O_WRONLY | O_CREAT | O_EXCL, 0600); | |
| 59 | |
| 60 return file_ != -1; | |
| 61 } | |
| 62 | |
| 63 bool MinidumpFileWriter::Close() { | |
| 64 bool result = true; | |
| 65 | |
| 66 if (file_ != -1) { | |
| 67 ftruncate(file_, position_); | |
| 68 result = (sys_close(file_) == 0); | |
| 69 file_ = -1; | |
| 70 } | |
| 71 | |
| 72 return result; | |
| 73 } | |
| 74 | |
| 75 bool MinidumpFileWriter::CopyStringToMDString(const wchar_t *str, | |
| 76 unsigned int length, | |
| 77 TypedMDRVA<MDString> *mdstring) { | |
| 78 bool result = true; | |
| 79 if (sizeof(wchar_t) == sizeof(u_int16_t)) { | |
| 80 // Shortcut if wchar_t is the same size as MDString's buffer | |
| 81 result = mdstring->Copy(str, mdstring->get()->length); | |
| 82 } else { | |
| 83 u_int16_t out[2]; | |
| 84 int out_idx = 0; | |
| 85 | |
| 86 // Copy the string character by character | |
| 87 while (length && result) { | |
| 88 UTF32ToUTF16Char(*str, out); | |
| 89 if (!out[0]) | |
| 90 return false; | |
| 91 | |
| 92 // Process one character at a time | |
| 93 --length; | |
| 94 ++str; | |
| 95 | |
| 96 // Append the one or two UTF-16 characters. The first one will be non- | |
| 97 // zero, but the second one may be zero, depending on the conversion from | |
| 98 // UTF-32. | |
| 99 int out_count = out[1] ? 2 : 1; | |
| 100 int out_size = sizeof(u_int16_t) * out_count; | |
| 101 result = mdstring->CopyIndexAfterObject(out_idx, out, out_size); | |
| 102 out_idx += out_count; | |
| 103 } | |
| 104 } | |
| 105 return result; | |
| 106 } | |
| 107 | |
| 108 bool MinidumpFileWriter::CopyStringToMDString(const char *str, | |
| 109 unsigned int length, | |
| 110 TypedMDRVA<MDString> *mdstring) { | |
| 111 bool result = true; | |
| 112 u_int16_t out[2]; | |
| 113 int out_idx = 0; | |
| 114 | |
| 115 // Copy the string character by character | |
| 116 while (length && result) { | |
| 117 int conversion_count = UTF8ToUTF16Char(str, length, out); | |
| 118 if (!conversion_count) | |
| 119 return false; | |
| 120 | |
| 121 // Move the pointer along based on the nubmer of converted characters | |
| 122 length -= conversion_count; | |
| 123 str += conversion_count; | |
| 124 | |
| 125 // Append the one or two UTF-16 characters | |
| 126 int out_count = out[1] ? 2 : 1; | |
| 127 int out_size = sizeof(u_int16_t) * out_count; | |
| 128 result = mdstring->CopyIndexAfterObject(out_idx, out, out_size); | |
| 129 out_idx += out_count; | |
| 130 } | |
| 131 return result; | |
| 132 } | |
| 133 | |
| 134 template <typename CharType> | |
| 135 bool MinidumpFileWriter::WriteStringCore(const CharType *str, | |
| 136 unsigned int length, | |
| 137 MDLocationDescriptor *location) { | |
| 138 assert(str); | |
| 139 assert(location); | |
| 140 // Calculate the mdstring length by either limiting to |length| as passed in | |
| 141 // or by finding the location of the NULL character. | |
| 142 unsigned int mdstring_length = 0; | |
| 143 if (!length) | |
| 144 length = INT_MAX; | |
| 145 for (; mdstring_length < length && str[mdstring_length]; ++mdstring_length) | |
| 146 ; | |
| 147 | |
| 148 // Allocate the string buffer | |
| 149 TypedMDRVA<MDString> mdstring(this); | |
| 150 if (!mdstring.AllocateObjectAndArray(mdstring_length + 1, sizeof(u_int16_t))) | |
| 151 return false; | |
| 152 | |
| 153 // Set length excluding the NULL and copy the string | |
| 154 mdstring.get()->length = mdstring_length * sizeof(u_int16_t); | |
| 155 bool result = CopyStringToMDString(str, mdstring_length, &mdstring); | |
| 156 | |
| 157 // NULL terminate | |
| 158 if (result) { | |
| 159 u_int16_t ch = 0; | |
| 160 result = mdstring.CopyIndexAfterObject(mdstring_length, &ch, sizeof(ch)); | |
| 161 | |
| 162 if (result) | |
| 163 *location = mdstring.location(); | |
| 164 } | |
| 165 | |
| 166 return result; | |
| 167 } | |
| 168 | |
| 169 bool MinidumpFileWriter::WriteString(const wchar_t *str, unsigned int length, | |
| 170 MDLocationDescriptor *location) { | |
| 171 return WriteStringCore(str, length, location); | |
| 172 } | |
| 173 | |
| 174 bool MinidumpFileWriter::WriteString(const char *str, unsigned int length, | |
| 175 MDLocationDescriptor *location) { | |
| 176 return WriteStringCore(str, length, location); | |
| 177 } | |
| 178 | |
| 179 bool MinidumpFileWriter::WriteMemory(const void *src, size_t size, | |
| 180 MDMemoryDescriptor *output) { | |
| 181 assert(src); | |
| 182 assert(output); | |
| 183 UntypedMDRVA mem(this); | |
| 184 | |
| 185 if (!mem.Allocate(size)) | |
| 186 return false; | |
| 187 if (!mem.Copy(src, mem.size())) | |
| 188 return false; | |
| 189 | |
| 190 output->start_of_memory_range = reinterpret_cast<u_int64_t>(src); | |
| 191 output->memory = mem.location(); | |
| 192 | |
| 193 return true; | |
| 194 } | |
| 195 | |
| 196 MDRVA MinidumpFileWriter::Allocate(size_t size) { | |
| 197 assert(size); | |
| 198 assert(file_ != -1); | |
| 199 size_t aligned_size = (size + 7) & ~7; // 64-bit alignment | |
| 200 | |
| 201 if (position_ + aligned_size > size_) { | |
| 202 size_t growth = aligned_size; | |
| 203 size_t minimal_growth = getpagesize(); | |
| 204 | |
| 205 // Ensure that the file grows by at least the size of a memory page | |
| 206 if (growth < minimal_growth) | |
| 207 growth = minimal_growth; | |
| 208 | |
| 209 size_t new_size = size_ + growth; | |
| 210 if (ftruncate(file_, new_size) != 0) | |
| 211 return kInvalidMDRVA; | |
| 212 | |
| 213 size_ = new_size; | |
| 214 } | |
| 215 | |
| 216 MDRVA current_position = position_; | |
| 217 position_ += static_cast<MDRVA>(aligned_size); | |
| 218 | |
| 219 return current_position; | |
| 220 } | |
| 221 | |
| 222 bool MinidumpFileWriter::Copy(MDRVA position, const void *src, ssize_t size) { | |
| 223 assert(src); | |
| 224 assert(size); | |
| 225 assert(file_ != -1); | |
| 226 | |
| 227 // Ensure that the data will fit in the allocated space | |
| 228 if (size + position > size_) | |
| 229 return false; | |
| 230 | |
| 231 // Seek and write the data | |
| 232 if (sys_lseek(file_, position, SEEK_SET) == static_cast<off_t>(position)) { | |
| 233 if (sys_write(file_, src, size) == size) | |
| 234 return true; | |
| 235 } | |
| 236 | |
| 237 return false; | |
| 238 } | |
| 239 | |
| 240 bool UntypedMDRVA::Allocate(size_t size) { | |
| 241 assert(size_ == 0); | |
| 242 size_ = size; | |
| 243 position_ = writer_->Allocate(size_); | |
| 244 return position_ != MinidumpFileWriter::kInvalidMDRVA; | |
| 245 } | |
| 246 | |
| 247 bool UntypedMDRVA::Copy(MDRVA position, const void *src, size_t size) { | |
| 248 assert(src); | |
| 249 assert(size); | |
| 250 assert(position + size <= position_ + size_); | |
| 251 return writer_->Copy(position, src, size); | |
| 252 } | |
| 253 | |
| 254 } // namespace google_breakpad | |
| OLD | NEW |