File: FTLDWARFDebugLineInfo.cpp

package info (click to toggle)
webkit2gtk 2.6.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 115,572 kB
  • ctags: 216,388
  • sloc: cpp: 1,164,175; ansic: 18,422; perl: 16,884; python: 11,608; ruby: 9,409; xml: 8,376; asm: 4,765; yacc: 2,292; lex: 891; sh: 650; makefile: 79
file content (337 lines) | stat: -rw-r--r-- 11,351 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Copyright (C) 2014 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "FTLDWARFDebugLineInfo.h"

#if ENABLE(FTL_JIT)

#include <wtf/DataLog.h>

namespace JSC { namespace FTL {

DebugLineInterpreter::DebugLineInterpreter(const char* program)
    : m_program(program)
    , m_logResults(false)
{
    resetInterpreterState();
}

template <typename T> inline T read(const char*& program)
{
    T result = *reinterpret_cast<const T*>(program);
    program += sizeof(T);
    return result;
}

uint32_t DebugLineInterpreter::parseULEB128(const char*& offset)
{
    uint32_t result = 0;
    uint8_t byte;
    unsigned shiftAmount = 0;
    do {
        byte = read<uint8_t>(offset);
        result |= (byte & ~0x80) << shiftAmount;
        shiftAmount += 7;
    } while (byte & 0x80);
    return result;
}

int32_t DebugLineInterpreter::parseSLEB128(const char*& offset)
{
    int32_t result = 0;
    uint8_t byte;
    unsigned shiftAmount = 0;
    do {
        byte = read<uint8_t>(offset);
        result |= (byte & ~0x80) << shiftAmount;
        shiftAmount += 7;
    } while (byte & 0x80);
    
    // If the sign bit (in this case, the second MSB) on the last byte is set we need to zero extend.
    if (byte & 0x40)
        result |= -(1 << shiftAmount);
    return result;
}

void DebugLineInterpreter::run()
{
    parsePrologue();
    interpretStatementProgram();
    if (m_logResults)
        printLineInfo();
}

void DebugLineInterpreter::parsePrologue()
{
    const char* currentProgramOffset = m_program;
    m_prologue.totalLength = read<uint32_t>(currentProgramOffset);
    if (m_prologue.totalLength == 0xffffffff) {
        // This is 64-bit DWARF format.
        m_prologue.format = SixtyFourBit;
        m_prologue.totalLength = read<uint64_t>(currentProgramOffset);
    } else
        m_prologue.format = ThirtyTwoBit;
    m_prologue.version = read<uint16_t>(currentProgramOffset);
    
    if (m_prologue.format == ThirtyTwoBit)
        m_prologue.prologueLength = read<uint32_t>(currentProgramOffset);
    else
        m_prologue.prologueLength = read<uint64_t>(currentProgramOffset);
    const char* afterLengthOffset = currentProgramOffset;
    
    m_prologue.minimumInstructionLength = read<uint8_t>(currentProgramOffset);
    m_prologue.defaultIsStatement = read<uint8_t>(currentProgramOffset);
    m_prologue.lineBase = read<int8_t>(currentProgramOffset);
    m_prologue.lineRange = read<uint8_t>(currentProgramOffset);
    m_prologue.opcodeBase = read<uint8_t>(currentProgramOffset);
    for (unsigned i = 1; i < m_prologue.opcodeBase; ++i)
        m_prologue.standardOpcodeLengths.append(read<uint8_t>(currentProgramOffset));
    parseIncludeDirectories(currentProgramOffset);
    parseFileEntries(currentProgramOffset);
    
    m_program = afterLengthOffset + m_prologue.prologueLength;

    if (!m_logResults)
        return;

    dataLog("\nPrologue:\n");
    dataLog("totalLength = ", m_prologue.totalLength, "\n");
    dataLog("version = ", m_prologue.version, "\n");
    dataLog("prologueLength = ", m_prologue.prologueLength, "\n");
    dataLog("minimumInstructionLength = ", m_prologue.minimumInstructionLength, "\n");
    dataLog("defaultIsStatement = ", m_prologue.defaultIsStatement, "\n");
    dataLog("lineBase = ", m_prologue.lineBase, "\n");
    dataLog("lineRange = ", m_prologue.lineRange, "\n");
    dataLog("opcodeBase = ", m_prologue.opcodeBase, "\n");
    
    dataLog("\nStandard Opcode Lengths:\n");
    for (unsigned i = 1; i < m_prologue.opcodeBase; ++i)
        dataLog("standardOpcodeLengths[", i - 1, "] = ", m_prologue.standardOpcodeLengths[i - 1], "\n");
    
    dataLog("\nInclude Directories:\n");
    for (unsigned i = 0; i < m_prologue.includeDirectories.size(); ++i)
        dataLog("includeDirectories[", i, "] = ", m_prologue.includeDirectories[i], "\n");
    
    dataLog("\nFiles:\n");
    for (unsigned i = 0; i < m_prologue.fileEntries.size(); ++i) {
        FileEntry& entry = m_prologue.fileEntries[i];
        dataLog("fileEntries[", i, "] = {name: \"", entry.name, "\", dir_index: ", entry.directoryIndex, ", last_modified: ", entry.lastModified, ", size: ", entry.size, "}\n");
    }
}

void DebugLineInterpreter::parseIncludeDirectories(const char*& offset)
{
    size_t length = 0;
    while ((length = strlen(offset))) {
        m_prologue.includeDirectories.append(offset);
        offset += length + 1;
    }
    
    // Extra increment to get past the last null byte.
    offset += 1;
}

void DebugLineInterpreter::parseFileEntries(const char*& offset)
{
    while (true) {
        DebugLineInterpreter::FileEntry nextEntry;
        if (!parseFileEntry(offset, nextEntry))
            break;
        m_prologue.fileEntries.append(nextEntry);
    }
}

bool DebugLineInterpreter::parseFileEntry(const char*& offset, FileEntry& entry)
{
    size_t length = strlen(offset);
    if (!length) {
        offset += 1;
        return false;
    }
    entry.name = offset;
    offset += length + 1;
    entry.directoryIndex = parseULEB128(offset);
    entry.lastModified = parseULEB128(offset);
    entry.size = parseULEB128(offset);
    
    return true;
}

void DebugLineInterpreter::interpretStatementProgram()
{
    const char* currentProgramOffset = m_program;
    bool keepGoing = true;
    do {
        keepGoing = interpretOpcode(currentProgramOffset);
    } while (keepGoing);
}

bool DebugLineInterpreter::interpretOpcode(const char*& offset)
{
    uint8_t nextOpcode = read<uint8_t>(offset);
    switch (nextOpcode) {
    case ExtendedOpcodes: {
        uint32_t length = parseULEB128(offset);
        if (!length)
            return false;
        uint8_t extendedOpcode = read<uint8_t>(offset);
        switch (extendedOpcode) {
        case DW_LNE_end_sequence: {
            m_currentState.endSequence = true;
            m_lineInfoMatrix.append(m_currentState);
            resetInterpreterState();
            break;
        }
        case DW_LNE_set_address: {
            m_currentState.address = read<size_t>(offset);
            break;
        }
        case DW_LNE_define_file: {
            fprintf(stderr, "Unimplemented extended opcode DW_LNE_define_file.\n");
            RELEASE_ASSERT_NOT_REACHED();
            break;
        }
        default: {
            fprintf(stderr, "Unknown extended opcode.\n");
            RELEASE_ASSERT_NOT_REACHED();
            break;
        }
        }
        break;
    }
        /* Standard opcodes */
    case DW_LNS_copy: {
        m_lineInfoMatrix.append(m_currentState);
        m_currentState.isBasicBlock = false;
        m_currentState.prologueEnd = false;
        m_currentState.epilogueBegin = false;
        break;
    }
    case DW_LNS_advance_pc: {
        uint32_t advance = parseULEB128(offset);
        m_currentState.address += advance * m_prologue.minimumInstructionLength;
        break;
    }
    case DW_LNS_advance_line: {
        int32_t advance = parseSLEB128(offset);
        m_currentState.line += advance;
        break;
    }
    case DW_LNS_set_file: {
        uint32_t fileIndex = parseULEB128(offset);
        m_currentState.file = fileIndex;
        break;
    }
    case DW_LNS_set_column: {
        m_currentState.column = parseULEB128(offset);
        break;
    }
    case DW_LNS_negate_stmt: {
        m_currentState.isStatement = !m_currentState.isStatement;
        break;
    }
    case DW_LNS_set_basic_block: {
        m_currentState.isBasicBlock = true;
        break;
    }
    case DW_LNS_const_add_pc: {
        uint8_t adjustedOpcode = nextOpcode - m_prologue.opcodeBase;
        uint32_t addressIncrement = (adjustedOpcode / m_prologue.lineRange) * m_prologue.minimumInstructionLength;
        m_currentState.address += addressIncrement;
        break;
    }
    case DW_LNS_fixed_advance_pc: {
        uint16_t advance = read<uint16_t>(offset);
        m_currentState.address += advance;
        break;
    }
    case DW_LNS_set_prologue_end: {
        m_currentState.prologueEnd = true;
        break;
    }
    case DW_LNS_set_epilogue_begin: {
        m_currentState.epilogueBegin = true;
        break;
    }
    case DW_LNS_set_isa: {
        m_currentState.isa = parseULEB128(offset);
        break;
    }
        /* Special opcodes */
    default: {
        uint8_t adjustedOpcode = nextOpcode - m_prologue.opcodeBase;
        uint32_t addressIncrement = (adjustedOpcode / m_prologue.lineRange) * m_prologue.minimumInstructionLength;
        int32_t lineIncrement = m_prologue.lineBase + (adjustedOpcode % m_prologue.lineRange);
        m_currentState.address += addressIncrement;
        m_currentState.line += lineIncrement;
        m_lineInfoMatrix.append(m_currentState);
        m_currentState.isBasicBlock = false;
        m_currentState.prologueEnd = false;
        m_currentState.epilogueBegin = false;
        break;
    }
    }
    return true;
}

void DebugLineInterpreter::printLineInfo()
{
    dataLog("\nLine Info Matrix:\n");
    for (unsigned i = 0; i < m_lineInfoMatrix.size(); ++i)
        printLineInfo(m_lineInfoMatrix[i]);
    dataLog("\n");
}

void DebugLineInterpreter::printLineInfo(LineInfo& info)
{
    dataLogF("address: %p", reinterpret_cast<void*>(info.address));
    dataLog("  file: ", info.file, "  line: ", info.line, "  column: ", info.column, "  isa: ", info.isa, "  ");
    dataLog("  statement?: ", info.isStatement);
    dataLog("  basic block?: ", info.isBasicBlock);
    dataLog("  end sequence?: ", info.endSequence);
    dataLog("  prologue end?: ", info.prologueEnd);
    dataLog("  epilogue begin?: ", info.epilogueBegin);
    dataLog("\n");
}

void DebugLineInterpreter::resetInterpreterState()
{
    m_currentState.address = 0;
    m_currentState.file = 1;
    m_currentState.line = 1;
    m_currentState.column = 0;
    m_currentState.isa = 0;
    m_currentState.isStatement = false;
    m_currentState.isBasicBlock = false;
    m_currentState.endSequence = false;
    m_currentState.prologueEnd = false;
    m_currentState.epilogueBegin = false;
}

} } // namespace JSC::FTL

#endif // ENABLE(FTL_JIT)