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 | // -*- mode: C++ -*- |
michael@0 | 2 | |
michael@0 | 3 | // Copyright (c) 2010, Google Inc. |
michael@0 | 4 | // All rights reserved. |
michael@0 | 5 | // |
michael@0 | 6 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | // modification, are permitted provided that the following conditions are |
michael@0 | 8 | // met: |
michael@0 | 9 | // |
michael@0 | 10 | // * Redistributions of source code must retain the above copyright |
michael@0 | 11 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | // * Redistributions in binary form must reproduce the above |
michael@0 | 13 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 14 | // in the documentation and/or other materials provided with the |
michael@0 | 15 | // distribution. |
michael@0 | 16 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 17 | // contributors may be used to endorse or promote products derived from |
michael@0 | 18 | // this software without specific prior written permission. |
michael@0 | 19 | // |
michael@0 | 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | |
michael@0 | 32 | // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
michael@0 | 33 | |
michael@0 | 34 | // synth_minidump.h: Interface to SynthMinidump: fake minidump generator. |
michael@0 | 35 | // |
michael@0 | 36 | // We treat a minidump file as the concatenation of a bunch of |
michael@0 | 37 | // test_assembler::Sections. The file header, stream directory, |
michael@0 | 38 | // streams, memory regions, strings, and so on --- each is a Section |
michael@0 | 39 | // that eventually gets appended to the minidump. Dump, Memory, |
michael@0 | 40 | // Context, Thread, and so on all inherit from test_assembler::Section. |
michael@0 | 41 | // For example: |
michael@0 | 42 | // |
michael@0 | 43 | // using google_breakpad::test_assembler::kLittleEndian; |
michael@0 | 44 | // using google_breakpad::SynthMinidump::Context; |
michael@0 | 45 | // using google_breakpad::SynthMinidump::Dump; |
michael@0 | 46 | // using google_breakpad::SynthMinidump::Memory; |
michael@0 | 47 | // using google_breakpad::SynthMinidump::Thread; |
michael@0 | 48 | // |
michael@0 | 49 | // Dump minidump(MD_NORMAL, kLittleEndian); |
michael@0 | 50 | // |
michael@0 | 51 | // Memory stack1(minidump, 0x569eb0a9); |
michael@0 | 52 | // ... build contents of stack1 with test_assembler::Section functions ... |
michael@0 | 53 | // |
michael@0 | 54 | // MDRawContextX86 x86_context1; |
michael@0 | 55 | // x86_context1.context_flags = MD_CONTEXT_X86; |
michael@0 | 56 | // x86_context1.eip = 0x7c90eb94; |
michael@0 | 57 | // x86_context1.esp = 0x569eb0a9; |
michael@0 | 58 | // x86_context1.ebp = x86_context1.esp + something appropriate; |
michael@0 | 59 | // Context context1(minidump, x86_context1); |
michael@0 | 60 | // |
michael@0 | 61 | // Thread thread1(minidump, 0xe4a4821d, stack1, context1); |
michael@0 | 62 | // |
michael@0 | 63 | // minidump.Add(&stack1); |
michael@0 | 64 | // minidump.Add(&context1); |
michael@0 | 65 | // minidump.Add(&thread1); |
michael@0 | 66 | // minidump.Finish(); |
michael@0 | 67 | // |
michael@0 | 68 | // string contents; |
michael@0 | 69 | // EXPECT_TRUE(minidump.GetContents(&contents)); |
michael@0 | 70 | // // contents now holds the bytes of a minidump file |
michael@0 | 71 | // |
michael@0 | 72 | // Because the test_assembler classes let us write Label references to |
michael@0 | 73 | // sections before the Labels' values are known, this gives us |
michael@0 | 74 | // flexibility in how we put the dump together: minidump pieces can |
michael@0 | 75 | // hold the file offsets of other minidump pieces before the |
michael@0 | 76 | // referents' positions have been decided. As long as everything has |
michael@0 | 77 | // been placed by the time we call dump.GetContents to obtain the |
michael@0 | 78 | // bytes, all the Labels' values will be known, and everything will |
michael@0 | 79 | // get patched up appropriately. |
michael@0 | 80 | // |
michael@0 | 81 | // The dump.Add(thing) functions append THINGS's contents to the |
michael@0 | 82 | // minidump, but they also do two other things: |
michael@0 | 83 | // |
michael@0 | 84 | // - dump.Add(thing) invokes thing->Finish, which tells *thing the |
michael@0 | 85 | // offset within the file at which it was placed, and allows *thing |
michael@0 | 86 | // to do any final content generation. |
michael@0 | 87 | // |
michael@0 | 88 | // - If THING is something which should receive an entry in some sort |
michael@0 | 89 | // of list or directory, then dump.Add(THING) automatically creates |
michael@0 | 90 | // the appropriate directory or list entry. Streams must appear in |
michael@0 | 91 | // the stream directory; memory ranges should be listed in the |
michael@0 | 92 | // memory list; threads should be placed in the thread list; and so |
michael@0 | 93 | // on. |
michael@0 | 94 | // |
michael@0 | 95 | // By convention, Section subclass constructors that take references |
michael@0 | 96 | // to other Sections do not take care of 'Add'ing their arguments to |
michael@0 | 97 | // the dump. For example, although the Thread constructor takes |
michael@0 | 98 | // references to a Memory and a Context, it does not add them to the |
michael@0 | 99 | // dump on the caller's behalf. Rather, the caller is responsible for |
michael@0 | 100 | // 'Add'ing every section they create. This allows Sections to be |
michael@0 | 101 | // cited from more than one place; for example, Memory ranges are |
michael@0 | 102 | // cited both from Thread objects (as their stack contents) and by the |
michael@0 | 103 | // memory list stream. |
michael@0 | 104 | // |
michael@0 | 105 | // If you forget to Add some Section, the Dump::GetContents call will |
michael@0 | 106 | // fail, as the test_assembler::Labels used to cite the Section's |
michael@0 | 107 | // contents from elsewhere will still be undefined. |
michael@0 | 108 | #ifndef PROCESSOR_SYNTH_MINIDUMP_H_ |
michael@0 | 109 | #define PROCESSOR_SYNTH_MINIDUMP_H_ |
michael@0 | 110 | |
michael@0 | 111 | #include <assert.h> |
michael@0 | 112 | |
michael@0 | 113 | #include <iostream> |
michael@0 | 114 | #include <string> |
michael@0 | 115 | |
michael@0 | 116 | #include "common/test_assembler.h" |
michael@0 | 117 | #include "common/using_std_string.h" |
michael@0 | 118 | #include "google_breakpad/common/breakpad_types.h" |
michael@0 | 119 | #include "google_breakpad/common/minidump_format.h" |
michael@0 | 120 | |
michael@0 | 121 | namespace google_breakpad { |
michael@0 | 122 | |
michael@0 | 123 | namespace SynthMinidump { |
michael@0 | 124 | |
michael@0 | 125 | using test_assembler::Endianness; |
michael@0 | 126 | using test_assembler::kBigEndian; |
michael@0 | 127 | using test_assembler::kLittleEndian; |
michael@0 | 128 | using test_assembler::kUnsetEndian; |
michael@0 | 129 | using test_assembler::Label; |
michael@0 | 130 | |
michael@0 | 131 | class Dump; |
michael@0 | 132 | class Memory; |
michael@0 | 133 | class String; |
michael@0 | 134 | |
michael@0 | 135 | // A test_assembler::Section which will be appended to a minidump. |
michael@0 | 136 | class Section: public test_assembler::Section { |
michael@0 | 137 | public: |
michael@0 | 138 | explicit Section(const Dump &dump); |
michael@0 | 139 | |
michael@0 | 140 | // Append an MDLocationDescriptor referring to this section to SECTION. |
michael@0 | 141 | // If 'this' is NULL, append a descriptor with a zero length and MDRVA. |
michael@0 | 142 | // |
michael@0 | 143 | // (I couldn't find the language in the C++ standard that says that |
michael@0 | 144 | // invoking member functions of a NULL pointer to a class type is |
michael@0 | 145 | // bad, if such language exists. Having this function handle NULL |
michael@0 | 146 | // 'this' is convenient, but if it causes trouble, it's not hard to |
michael@0 | 147 | // do differently.) |
michael@0 | 148 | void CiteLocationIn(test_assembler::Section *section) const; |
michael@0 | 149 | |
michael@0 | 150 | // Note that this section's contents are complete, and that it has |
michael@0 | 151 | // been placed in the minidump file at OFFSET. The 'Add' member |
michael@0 | 152 | // functions call the Finish member function of the object being |
michael@0 | 153 | // added for you; if you are 'Add'ing this section, you needn't Finish it. |
michael@0 | 154 | virtual void Finish(const Label &offset) { |
michael@0 | 155 | file_offset_ = offset; size_ = Size(); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | protected: |
michael@0 | 159 | // This section's size and offset within the minidump file. |
michael@0 | 160 | Label file_offset_, size_; |
michael@0 | 161 | }; |
michael@0 | 162 | |
michael@0 | 163 | // A stream within a minidump file. 'Add'ing a stream to a minidump |
michael@0 | 164 | // creates an entry for it in the minidump's stream directory. |
michael@0 | 165 | class Stream: public Section { |
michael@0 | 166 | public: |
michael@0 | 167 | // Create a stream of type TYPE. You can append whatever contents |
michael@0 | 168 | // you like to this stream using the test_assembler::Section methods. |
michael@0 | 169 | Stream(const Dump &dump, uint32_t type) : Section(dump), type_(type) { } |
michael@0 | 170 | |
michael@0 | 171 | // Append an MDRawDirectory referring to this stream to SECTION. |
michael@0 | 172 | void CiteStreamIn(test_assembler::Section *section) const; |
michael@0 | 173 | |
michael@0 | 174 | private: |
michael@0 | 175 | // The type of this stream. |
michael@0 | 176 | uint32_t type_; |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | class SystemInfo: public Stream { |
michael@0 | 180 | public: |
michael@0 | 181 | // Create an MD_SYSTEM_INFO_STREAM stream belonging to DUMP holding |
michael@0 | 182 | // an MDRawSystem info structure initialized with the values from |
michael@0 | 183 | // SYSTEM_INFO, except that the csd_version field is replaced with |
michael@0 | 184 | // the file offset of the string CSD_VERSION, which can be 'Add'ed |
michael@0 | 185 | // to the dump at the desired location. |
michael@0 | 186 | // |
michael@0 | 187 | // Remember that you are still responsible for 'Add'ing CSD_VERSION |
michael@0 | 188 | // to the dump yourself. |
michael@0 | 189 | SystemInfo(const Dump &dump, |
michael@0 | 190 | const MDRawSystemInfo &system_info, |
michael@0 | 191 | const String &csd_version); |
michael@0 | 192 | |
michael@0 | 193 | // Stock MDRawSystemInfo information and associated strings, for |
michael@0 | 194 | // writing tests. |
michael@0 | 195 | static const MDRawSystemInfo windows_x86; |
michael@0 | 196 | static const string windows_x86_csd_version; |
michael@0 | 197 | }; |
michael@0 | 198 | |
michael@0 | 199 | // An MDString: a string preceded by a 32-bit length. |
michael@0 | 200 | class String: public Section { |
michael@0 | 201 | public: |
michael@0 | 202 | String(const Dump &dump, const string &value); |
michael@0 | 203 | |
michael@0 | 204 | // Append an MDRVA referring to this string to SECTION. |
michael@0 | 205 | void CiteStringIn(test_assembler::Section *section) const; |
michael@0 | 206 | }; |
michael@0 | 207 | |
michael@0 | 208 | // A range of memory contents. 'Add'ing a memory range to a minidump |
michael@0 | 209 | // creates n entry for it in the minidump's memory list. By |
michael@0 | 210 | // convention, the 'start', 'Here', and 'Mark' member functions refer |
michael@0 | 211 | // to memory addresses. |
michael@0 | 212 | class Memory: public Section { |
michael@0 | 213 | public: |
michael@0 | 214 | Memory(const Dump &dump, uint64_t address) |
michael@0 | 215 | : Section(dump), address_(address) { start() = address; } |
michael@0 | 216 | |
michael@0 | 217 | // Append an MDMemoryDescriptor referring to this memory range to SECTION. |
michael@0 | 218 | void CiteMemoryIn(test_assembler::Section *section) const; |
michael@0 | 219 | |
michael@0 | 220 | private: |
michael@0 | 221 | // The process address from which these memory contents were taken. |
michael@0 | 222 | // Shouldn't this be a Label? |
michael@0 | 223 | uint64_t address_; |
michael@0 | 224 | }; |
michael@0 | 225 | |
michael@0 | 226 | class Context: public Section { |
michael@0 | 227 | public: |
michael@0 | 228 | // Create a context belonging to DUMP whose contents are a copy of CONTEXT. |
michael@0 | 229 | Context(const Dump &dump, const MDRawContextX86 &context); |
michael@0 | 230 | Context(const Dump &dump, const MDRawContextARM &context); |
michael@0 | 231 | // Add an empty context to the dump. |
michael@0 | 232 | Context(const Dump &dump) : Section(dump) {} |
michael@0 | 233 | // Add constructors for other architectures here. Remember to byteswap. |
michael@0 | 234 | }; |
michael@0 | 235 | |
michael@0 | 236 | class Thread: public Section { |
michael@0 | 237 | public: |
michael@0 | 238 | // Create a thread belonging to DUMP with the given values, citing |
michael@0 | 239 | // STACK and CONTEXT (which you must Add to the dump separately). |
michael@0 | 240 | Thread(const Dump &dump, |
michael@0 | 241 | uint32_t thread_id, |
michael@0 | 242 | const Memory &stack, |
michael@0 | 243 | const Context &context, |
michael@0 | 244 | uint32_t suspend_count = 0, |
michael@0 | 245 | uint32_t priority_class = 0, |
michael@0 | 246 | uint32_t priority = 0, |
michael@0 | 247 | uint64_t teb = 0); |
michael@0 | 248 | }; |
michael@0 | 249 | |
michael@0 | 250 | class Module: public Section { |
michael@0 | 251 | public: |
michael@0 | 252 | // Create a module with the given values. Note that CV_RECORD and |
michael@0 | 253 | // MISC_RECORD can be NULL, in which case the corresponding location |
michael@0 | 254 | // descriptior in the minidump will have a length of zero. |
michael@0 | 255 | Module(const Dump &dump, |
michael@0 | 256 | uint64_t base_of_image, |
michael@0 | 257 | uint32_t size_of_image, |
michael@0 | 258 | const String &name, |
michael@0 | 259 | uint32_t time_date_stamp = 1262805309, |
michael@0 | 260 | uint32_t checksum = 0, |
michael@0 | 261 | const MDVSFixedFileInfo &version_info = Module::stock_version_info, |
michael@0 | 262 | const Section *cv_record = NULL, |
michael@0 | 263 | const Section *misc_record = NULL); |
michael@0 | 264 | |
michael@0 | 265 | private: |
michael@0 | 266 | // A standard MDVSFixedFileInfo structure to use as a default for |
michael@0 | 267 | // minidumps. There's no reason to make users write out all this crap |
michael@0 | 268 | // over and over. |
michael@0 | 269 | static const MDVSFixedFileInfo stock_version_info; |
michael@0 | 270 | }; |
michael@0 | 271 | |
michael@0 | 272 | class Exception : public Stream { |
michael@0 | 273 | public: |
michael@0 | 274 | Exception(const Dump &dump, |
michael@0 | 275 | const Context &context, |
michael@0 | 276 | uint32_t thread_id = 0, |
michael@0 | 277 | uint32_t exception_code = 0, |
michael@0 | 278 | uint32_t exception_flags = 0, |
michael@0 | 279 | uint64_t exception_address = 0); |
michael@0 | 280 | }; |
michael@0 | 281 | |
michael@0 | 282 | // A list of entries starting with a 32-bit count, like a memory list |
michael@0 | 283 | // or a thread list. |
michael@0 | 284 | template<typename Element> |
michael@0 | 285 | class List: public Stream { |
michael@0 | 286 | public: |
michael@0 | 287 | List(const Dump &dump, uint32_t type) : Stream(dump, type), count_(0) { |
michael@0 | 288 | D32(count_label_); |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | // Add ELEMENT to this list. |
michael@0 | 292 | void Add(Element *element) { |
michael@0 | 293 | element->Finish(file_offset_ + Size()); |
michael@0 | 294 | Append(*element); |
michael@0 | 295 | count_++; |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | // Return true if this List is empty, false otherwise. |
michael@0 | 299 | bool Empty() { return count_ == 0; } |
michael@0 | 300 | |
michael@0 | 301 | // Finish up the contents of this section, mark it as having been |
michael@0 | 302 | // placed at OFFSET. |
michael@0 | 303 | virtual void Finish(const Label &offset) { |
michael@0 | 304 | Stream::Finish(offset); |
michael@0 | 305 | count_label_ = count_; |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | private: |
michael@0 | 309 | size_t count_; |
michael@0 | 310 | Label count_label_; |
michael@0 | 311 | }; |
michael@0 | 312 | |
michael@0 | 313 | class Dump: public test_assembler::Section { |
michael@0 | 314 | public: |
michael@0 | 315 | |
michael@0 | 316 | // Create a test_assembler::Section containing a minidump file whose |
michael@0 | 317 | // header uses the given values. ENDIANNESS determines the |
michael@0 | 318 | // endianness of the signature; we set this section's default |
michael@0 | 319 | // endianness by this. |
michael@0 | 320 | Dump(uint64_t flags, |
michael@0 | 321 | Endianness endianness = kLittleEndian, |
michael@0 | 322 | uint32_t version = MD_HEADER_VERSION, |
michael@0 | 323 | uint32_t date_time_stamp = 1262805309); |
michael@0 | 324 | |
michael@0 | 325 | // The following functions call OBJECT->Finish(), and append the |
michael@0 | 326 | // contents of OBJECT to this minidump. They also record OBJECT in |
michael@0 | 327 | // whatever directory or list is appropriate for its type. The |
michael@0 | 328 | // stream directory, memory list, thread list, and module list are |
michael@0 | 329 | // accumulated this way. |
michael@0 | 330 | Dump &Add(SynthMinidump::Section *object); // simply append data |
michael@0 | 331 | Dump &Add(Stream *object); // append, record in stream directory |
michael@0 | 332 | Dump &Add(Memory *object); // append, record in memory list |
michael@0 | 333 | Dump &Add(Thread *object); // append, record in thread list |
michael@0 | 334 | Dump &Add(Module *object); // append, record in module list |
michael@0 | 335 | |
michael@0 | 336 | // Complete the construction of the minidump, given the Add calls |
michael@0 | 337 | // we've seen up to this point. After this call, this Dump's |
michael@0 | 338 | // contents are complete, all labels should be defined if everything |
michael@0 | 339 | // Cited has been Added, and you may call GetContents on it. |
michael@0 | 340 | void Finish(); |
michael@0 | 341 | |
michael@0 | 342 | private: |
michael@0 | 343 | // A label representing the start of the minidump file. |
michael@0 | 344 | Label file_start_; |
michael@0 | 345 | |
michael@0 | 346 | // The stream directory. We construct this incrementally from |
michael@0 | 347 | // Add(Stream *) calls. |
michael@0 | 348 | SynthMinidump::Section stream_directory_; // The directory's contents. |
michael@0 | 349 | size_t stream_count_; // The number of streams so far. |
michael@0 | 350 | Label stream_count_label_; // Cited in file header. |
michael@0 | 351 | Label stream_directory_rva_; // The directory's file offset. |
michael@0 | 352 | |
michael@0 | 353 | // This minidump's thread list. We construct this incrementally from |
michael@0 | 354 | // Add(Thread *) calls. |
michael@0 | 355 | List<Thread> thread_list_; |
michael@0 | 356 | |
michael@0 | 357 | // This minidump's module list. We construct this incrementally from |
michael@0 | 358 | // Add(Module *) calls. |
michael@0 | 359 | List<Module> module_list_; |
michael@0 | 360 | |
michael@0 | 361 | // This minidump's memory list. We construct this incrementally from |
michael@0 | 362 | // Add(Memory *) calls. This is actually a list of MDMemoryDescriptors, |
michael@0 | 363 | // not memory ranges --- thus the odd type. |
michael@0 | 364 | List<SynthMinidump::Section> memory_list_; |
michael@0 | 365 | }; |
michael@0 | 366 | |
michael@0 | 367 | } // namespace SynthMinidump |
michael@0 | 368 | |
michael@0 | 369 | } // namespace google_breakpad |
michael@0 | 370 | |
michael@0 | 371 | #endif // PROCESSOR_SYNTH_MINIDUMP_H_ |