tools/profiler/LulMainInt.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef LulMainInt_h
michael@0 8 #define LulMainInt_h
michael@0 9
michael@0 10 #include "LulPlatformMacros.h"
michael@0 11
michael@0 12 #include <vector>
michael@0 13
michael@0 14 #include "mozilla/Assertions.h"
michael@0 15
michael@0 16 // This file is provides internal interface inside LUL. If you are an
michael@0 17 // end-user of LUL, do not include it in your code. The end-user
michael@0 18 // interface is in LulMain.h.
michael@0 19
michael@0 20
michael@0 21 namespace lul {
michael@0 22
michael@0 23 ////////////////////////////////////////////////////////////////
michael@0 24 // DW_REG_ constants //
michael@0 25 ////////////////////////////////////////////////////////////////
michael@0 26
michael@0 27 // These are the Dwarf CFI register numbers, as (presumably) defined
michael@0 28 // in the ELF ABI supplements for each architecture.
michael@0 29
michael@0 30 enum DW_REG_NUMBER {
michael@0 31 // No real register has this number. It's convenient to be able to
michael@0 32 // treat the CFA (Canonical Frame Address) as "just another
michael@0 33 // register", though.
michael@0 34 DW_REG_CFA = -1,
michael@0 35 #if defined(LUL_ARCH_arm)
michael@0 36 // ARM registers
michael@0 37 DW_REG_ARM_R7 = 7,
michael@0 38 DW_REG_ARM_R11 = 11,
michael@0 39 DW_REG_ARM_R12 = 12,
michael@0 40 DW_REG_ARM_R13 = 13,
michael@0 41 DW_REG_ARM_R14 = 14,
michael@0 42 DW_REG_ARM_R15 = 15,
michael@0 43 #elif defined(LUL_ARCH_x64)
michael@0 44 // Because the X86 (32 bit) and AMD64 (64 bit) summarisers are
michael@0 45 // combined, a merged set of register constants is needed.
michael@0 46 DW_REG_INTEL_XBP = 6,
michael@0 47 DW_REG_INTEL_XSP = 7,
michael@0 48 DW_REG_INTEL_XIP = 16,
michael@0 49 #elif defined(LUL_ARCH_x86)
michael@0 50 DW_REG_INTEL_XBP = 5,
michael@0 51 DW_REG_INTEL_XSP = 4,
michael@0 52 DW_REG_INTEL_XIP = 8,
michael@0 53 #else
michael@0 54 # error "Unknown arch"
michael@0 55 #endif
michael@0 56 };
michael@0 57
michael@0 58
michael@0 59 ////////////////////////////////////////////////////////////////
michael@0 60 // LExpr //
michael@0 61 ////////////////////////////////////////////////////////////////
michael@0 62
michael@0 63 // An expression -- very primitive. Denotes either "register +
michael@0 64 // offset" or a dereferenced version of the same. So as to allow
michael@0 65 // convenient handling of Dwarf-derived unwind info, the register may
michael@0 66 // also denote the CFA. A large number of these need to be stored, so
michael@0 67 // we ensure it fits into 8 bytes. See comment below on RuleSet to
michael@0 68 // see how expressions fit into the bigger picture.
michael@0 69
michael@0 70 struct LExpr {
michael@0 71 // Denotes an expression with no value.
michael@0 72 LExpr()
michael@0 73 : mHow(UNKNOWN)
michael@0 74 , mReg(0)
michael@0 75 , mOffset(0)
michael@0 76 {}
michael@0 77
michael@0 78 // Denotes any expressible expression.
michael@0 79 LExpr(uint8_t how, int16_t reg, int32_t offset)
michael@0 80 : mHow(how)
michael@0 81 , mReg(reg)
michael@0 82 , mOffset(offset)
michael@0 83 {}
michael@0 84
michael@0 85 // Change the offset for an expression that references memory.
michael@0 86 LExpr add_delta(long delta)
michael@0 87 {
michael@0 88 MOZ_ASSERT(mHow == NODEREF);
michael@0 89 // If this is a non-debug build and the above assertion would have
michael@0 90 // failed, at least return LExpr() so that the machinery that uses
michael@0 91 // the resulting expression fails in a repeatable way.
michael@0 92 return (mHow == NODEREF) ? LExpr(mHow, mReg, mOffset+delta)
michael@0 93 : LExpr(); // Gone bad
michael@0 94 }
michael@0 95
michael@0 96 // Dereference an expression that denotes a memory address.
michael@0 97 LExpr deref()
michael@0 98 {
michael@0 99 MOZ_ASSERT(mHow == NODEREF);
michael@0 100 // Same rationale as for add_delta().
michael@0 101 return (mHow == NODEREF) ? LExpr(DEREF, mReg, mOffset)
michael@0 102 : LExpr(); // Gone bad
michael@0 103 }
michael@0 104
michael@0 105 // Representation of expressions. If |mReg| is DW_REG_CFA (-1) then
michael@0 106 // it denotes the CFA. All other allowed values for |mReg| are
michael@0 107 // nonnegative and are DW_REG_ values.
michael@0 108
michael@0 109 enum { UNKNOWN=0, // This LExpr denotes no value.
michael@0 110 NODEREF, // Value is (mReg + mOffset).
michael@0 111 DEREF }; // Value is *(mReg + mOffset).
michael@0 112
michael@0 113 uint8_t mHow; // UNKNOWN, NODEREF or DEREF
michael@0 114 int16_t mReg; // A DW_REG_ value
michael@0 115 int32_t mOffset; // 32-bit signed offset should be more than enough.
michael@0 116 };
michael@0 117
michael@0 118 static_assert(sizeof(LExpr) <= 8, "LExpr size changed unexpectedly");
michael@0 119
michael@0 120
michael@0 121 ////////////////////////////////////////////////////////////////
michael@0 122 // RuleSet //
michael@0 123 ////////////////////////////////////////////////////////////////
michael@0 124
michael@0 125 // This is platform-dependent. For some address range, describes how
michael@0 126 // to recover the CFA and then how to recover the registers for the
michael@0 127 // previous frame.
michael@0 128 //
michael@0 129 // The set of LExprs contained in a given RuleSet describe a DAG which
michael@0 130 // says how to compute the caller's registers ("new registers") from
michael@0 131 // the callee's registers ("old registers"). The DAG can contain a
michael@0 132 // single internal node, which is the value of the CFA for the callee.
michael@0 133 // It would be possible to construct a DAG that omits the CFA, but
michael@0 134 // including it makes the summarisers simpler, and the Dwarf CFI spec
michael@0 135 // has the CFA as a central concept.
michael@0 136 //
michael@0 137 // For this to make sense, |mCfaExpr| can't have
michael@0 138 // |mReg| == DW_REG_CFA since we have no previous value for the CFA.
michael@0 139 // All of the other |Expr| fields can -- and usually do -- specify
michael@0 140 // |mReg| == DW_REG_CFA.
michael@0 141 //
michael@0 142 // With that in place, the unwind algorithm proceeds as follows.
michael@0 143 //
michael@0 144 // (0) Initially: we have values for the old registers, and a memory
michael@0 145 // image.
michael@0 146 //
michael@0 147 // (1) Compute the CFA by evaluating |mCfaExpr|. Add the computed
michael@0 148 // value to the set of "old registers".
michael@0 149 //
michael@0 150 // (2) Compute values for the registers by evaluating all of the other
michael@0 151 // |Expr| fields in the RuleSet. These can depend on both the old
michael@0 152 // register values and the just-computed CFA.
michael@0 153 //
michael@0 154 // If we are unwinding without computing a CFA, perhaps because the
michael@0 155 // RuleSets are derived from EXIDX instead of Dwarf, then
michael@0 156 // |mCfaExpr.mHow| will be LExpr::UNKNOWN, so the computed value will
michael@0 157 // be invalid -- that is, TaggedUWord() -- and so any attempt to use
michael@0 158 // that will result in the same value. But that's OK because the
michael@0 159 // RuleSet would make no sense if depended on the CFA but specified no
michael@0 160 // way to compute it.
michael@0 161 //
michael@0 162 // A RuleSet is not allowed to cover zero address range. Having zero
michael@0 163 // length would break binary searching in SecMaps and PriMaps.
michael@0 164
michael@0 165 class RuleSet {
michael@0 166 public:
michael@0 167 RuleSet();
michael@0 168 void Print(void(*aLog)(const char*));
michael@0 169
michael@0 170 // Find the LExpr* for a given DW_REG_ value in this class.
michael@0 171 LExpr* ExprForRegno(DW_REG_NUMBER aRegno);
michael@0 172
michael@0 173 uintptr_t mAddr;
michael@0 174 uintptr_t mLen;
michael@0 175 // How to compute the CFA.
michael@0 176 LExpr mCfaExpr;
michael@0 177 // How to compute caller register values. These may reference the
michael@0 178 // value defined by |mCfaExpr|.
michael@0 179 #if defined(LUL_ARCH_x64) || defined(LUL_ARCH_x86)
michael@0 180 LExpr mXipExpr; // return address
michael@0 181 LExpr mXspExpr;
michael@0 182 LExpr mXbpExpr;
michael@0 183 #elif defined(LUL_ARCH_arm)
michael@0 184 LExpr mR15expr; // return address
michael@0 185 LExpr mR14expr;
michael@0 186 LExpr mR13expr;
michael@0 187 LExpr mR12expr;
michael@0 188 LExpr mR11expr;
michael@0 189 LExpr mR7expr;
michael@0 190 #else
michael@0 191 # error "Unknown arch"
michael@0 192 #endif
michael@0 193 };
michael@0 194
michael@0 195
michael@0 196 ////////////////////////////////////////////////////////////////
michael@0 197 // SecMap //
michael@0 198 ////////////////////////////////////////////////////////////////
michael@0 199
michael@0 200 // A SecMap may have zero address range, temporarily, whilst RuleSets
michael@0 201 // are being added to it. But adding a zero-range SecMap to a PriMap
michael@0 202 // will make it impossible to maintain the total order of the PriMap
michael@0 203 // entries, and so that can't be allowed to happen.
michael@0 204
michael@0 205 class SecMap {
michael@0 206 public:
michael@0 207 // These summarise the contained mRuleSets, in that they give
michael@0 208 // exactly the lowest and highest addresses that any of the entries
michael@0 209 // in this SecMap cover. Hence invariants:
michael@0 210 //
michael@0 211 // mRuleSets is nonempty
michael@0 212 // <=> mSummaryMinAddr <= mSummaryMaxAddr
michael@0 213 // && mSummaryMinAddr == mRuleSets[0].mAddr
michael@0 214 // && mSummaryMaxAddr == mRuleSets[#rulesets-1].mAddr
michael@0 215 // + mRuleSets[#rulesets-1].mLen - 1;
michael@0 216 //
michael@0 217 // This requires that no RuleSet has zero length.
michael@0 218 //
michael@0 219 // mRuleSets is empty
michael@0 220 // <=> mSummaryMinAddr > mSummaryMaxAddr
michael@0 221 //
michael@0 222 // This doesn't constrain mSummaryMinAddr and mSummaryMaxAddr uniquely,
michael@0 223 // so let's use mSummaryMinAddr == 1 and mSummaryMaxAddr == 0 to denote
michael@0 224 // this case.
michael@0 225
michael@0 226 SecMap(void(*aLog)(const char*));
michael@0 227 ~SecMap();
michael@0 228
michael@0 229 // Binary search mRuleSets to find one that brackets |ia|, or nullptr
michael@0 230 // if none is found. It's not allowable to do this until PrepareRuleSets
michael@0 231 // has been called first.
michael@0 232 RuleSet* FindRuleSet(uintptr_t ia);
michael@0 233
michael@0 234 // Add a RuleSet to the collection. The rule is copied in. Calling
michael@0 235 // this makes the map non-searchable.
michael@0 236 void AddRuleSet(RuleSet* rs);
michael@0 237
michael@0 238 // Prepare the map for searching. Also, remove any rules for code
michael@0 239 // address ranges which don't fall inside [start, +len). |len| may
michael@0 240 // not be zero.
michael@0 241 void PrepareRuleSets(uintptr_t start, size_t len);
michael@0 242
michael@0 243 bool IsEmpty();
michael@0 244
michael@0 245 size_t Size() { return mRuleSets.size(); }
michael@0 246
michael@0 247 // The min and max addresses of the addresses in the contained
michael@0 248 // RuleSets. See comment above for invariants.
michael@0 249 uintptr_t mSummaryMinAddr;
michael@0 250 uintptr_t mSummaryMaxAddr;
michael@0 251
michael@0 252 private:
michael@0 253 // False whilst adding entries; true once it is safe to call FindRuleSet.
michael@0 254 // Transition (false->true) is caused by calling PrepareRuleSets().
michael@0 255 bool mUsable;
michael@0 256
michael@0 257 // A vector of RuleSets, sorted, nonoverlapping (post Prepare()).
michael@0 258 std::vector<RuleSet> mRuleSets;
michael@0 259
michael@0 260 // A logging sink, for debugging.
michael@0 261 void (*mLog)(const char*);
michael@0 262 };
michael@0 263
michael@0 264 } // namespace lul
michael@0 265
michael@0 266 #endif // ndef LulMainInt_h

mercurial