1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/dwarf/dwarf2reader_test_common.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,149 @@ 1.4 +// -*- mode: c++ -*- 1.5 + 1.6 +// Copyright (c) 2012, Google Inc. 1.7 +// All rights reserved. 1.8 +// 1.9 +// Redistribution and use in source and binary forms, with or without 1.10 +// modification, are permitted provided that the following conditions are 1.11 +// met: 1.12 +// 1.13 +// * Redistributions of source code must retain the above copyright 1.14 +// notice, this list of conditions and the following disclaimer. 1.15 +// * Redistributions in binary form must reproduce the above 1.16 +// copyright notice, this list of conditions and the following disclaimer 1.17 +// in the documentation and/or other materials provided with the 1.18 +// distribution. 1.19 +// * Neither the name of Google Inc. nor the names of its 1.20 +// contributors may be used to endorse or promote products derived from 1.21 +// this software without specific prior written permission. 1.22 +// 1.23 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.27 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.28 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.29 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.30 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.31 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.32 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.33 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + 1.35 +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> 1.36 + 1.37 +// dwarf2reader_test_common.h: Define TestCompilationUnit and 1.38 +// TestAbbrevTable, classes for creating properly (and improperly) 1.39 +// formatted DWARF compilation unit data for unit tests. 1.40 + 1.41 +#ifndef COMMON_DWARF_DWARF2READER_TEST_COMMON_H__ 1.42 +#define COMMON_DWARF_DWARF2READER_TEST_COMMON_H__ 1.43 + 1.44 +#include "common/test_assembler.h" 1.45 +#include "common/dwarf/dwarf2enums.h" 1.46 + 1.47 +// A subclass of test_assembler::Section, specialized for constructing 1.48 +// DWARF compilation units. 1.49 +class TestCompilationUnit: public google_breakpad::test_assembler::Section { 1.50 + public: 1.51 + typedef dwarf2reader::DwarfTag DwarfTag; 1.52 + typedef dwarf2reader::DwarfAttribute DwarfAttribute; 1.53 + typedef dwarf2reader::DwarfForm DwarfForm; 1.54 + typedef google_breakpad::test_assembler::Label Label; 1.55 + 1.56 + // Set the section's DWARF format size (the 32-bit DWARF format or the 1.57 + // 64-bit DWARF format, for lengths and section offsets --- not the 1.58 + // address size) to format_size. 1.59 + void set_format_size(size_t format_size) { 1.60 + assert(format_size == 4 || format_size == 8); 1.61 + format_size_ = format_size; 1.62 + } 1.63 + 1.64 + // Append a DWARF section offset value, of the appropriate size for this 1.65 + // compilation unit. 1.66 + template<typename T> 1.67 + void SectionOffset(T offset) { 1.68 + if (format_size_ == 4) 1.69 + D32(offset); 1.70 + else 1.71 + D64(offset); 1.72 + } 1.73 + 1.74 + // Append a DWARF compilation unit header to the section, with the given 1.75 + // DWARF version, abbrev table offset, and address size. 1.76 + TestCompilationUnit &Header(int version, const Label &abbrev_offset, 1.77 + size_t address_size) { 1.78 + if (format_size_ == 4) { 1.79 + D32(length_); 1.80 + } else { 1.81 + D32(0xffffffff); 1.82 + D64(length_); 1.83 + } 1.84 + post_length_offset_ = Size(); 1.85 + D16(version); 1.86 + SectionOffset(abbrev_offset); 1.87 + D8(address_size); 1.88 + return *this; 1.89 + } 1.90 + 1.91 + // Mark the end of this header's DIEs. 1.92 + TestCompilationUnit &Finish() { 1.93 + length_ = Size() - post_length_offset_; 1.94 + return *this; 1.95 + } 1.96 + 1.97 + private: 1.98 + // The DWARF format size for this compilation unit. 1.99 + size_t format_size_; 1.100 + 1.101 + // The offset of the point in the compilation unit header immediately 1.102 + // after the initial length field. 1.103 + uint64_t post_length_offset_; 1.104 + 1.105 + // The length of the compilation unit, not including the initial length field. 1.106 + Label length_; 1.107 +}; 1.108 + 1.109 +// A subclass of test_assembler::Section specialized for constructing DWARF 1.110 +// abbreviation tables. 1.111 +class TestAbbrevTable: public google_breakpad::test_assembler::Section { 1.112 + public: 1.113 + typedef dwarf2reader::DwarfTag DwarfTag; 1.114 + typedef dwarf2reader::DwarfAttribute DwarfAttribute; 1.115 + typedef dwarf2reader::DwarfForm DwarfForm; 1.116 + typedef dwarf2reader::DwarfHasChild DwarfHasChild; 1.117 + typedef google_breakpad::test_assembler::Label Label; 1.118 + 1.119 + // Start a new abbreviation table entry for abbreviation code |code|, 1.120 + // encoding a DIE whose tag is |tag|, and which has children if and only 1.121 + // if |has_children| is true. 1.122 + TestAbbrevTable &Abbrev(int code, DwarfTag tag, DwarfHasChild has_children) { 1.123 + assert(code != 0); 1.124 + ULEB128(code); 1.125 + ULEB128(static_cast<unsigned>(tag)); 1.126 + D8(static_cast<unsigned>(has_children)); 1.127 + return *this; 1.128 + }; 1.129 + 1.130 + // Add an attribute to the current abbreviation code whose name is |name| 1.131 + // and whose form is |form|. 1.132 + TestAbbrevTable &Attribute(DwarfAttribute name, DwarfForm form) { 1.133 + ULEB128(static_cast<unsigned>(name)); 1.134 + ULEB128(static_cast<unsigned>(form)); 1.135 + return *this; 1.136 + } 1.137 + 1.138 + // Finish the current abbreviation code. 1.139 + TestAbbrevTable &EndAbbrev() { 1.140 + ULEB128(0); 1.141 + ULEB128(0); 1.142 + return *this; 1.143 + } 1.144 + 1.145 + // Finish the current abbreviation table. 1.146 + TestAbbrevTable &EndTable() { 1.147 + ULEB128(0); 1.148 + return *this; 1.149 + } 1.150 +}; 1.151 + 1.152 +#endif // COMMON_DWARF_DWARF2READER_TEST_COMMON_H__