1
0
mirror of https://github.com/beego/bee.git synced 2024-11-15 13:40:54 +00:00
bee/vendor/github.com/go-delve/delve/pkg/dwarf/frame/parser.go

135 lines
3.4 KiB
Go
Raw Normal View History

2017-03-19 22:45:54 +00:00
// Package frame contains data structures and
// related functions for parsing and searching
// through Dwarf .debug_frame data.
package frame
import (
"bytes"
"encoding/binary"
2019-04-15 14:43:01 +00:00
"github.com/go-delve/delve/pkg/dwarf/util"
2017-03-19 22:45:54 +00:00
)
type parsefunc func(*parseContext) parsefunc
type parseContext struct {
2018-10-13 13:45:53 +00:00
staticBase uint64
2017-03-19 22:45:54 +00:00
buf *bytes.Buffer
entries FrameDescriptionEntries
common *CommonInformationEntry
frame *FrameDescriptionEntry
length uint32
}
// Parse takes in data (a byte slice) and returns a slice of
// commonInformationEntry structures. Each commonInformationEntry
// has a slice of frameDescriptionEntry structures.
2018-10-13 13:45:53 +00:00
func Parse(data []byte, order binary.ByteOrder, staticBase uint64) FrameDescriptionEntries {
2017-03-19 22:45:54 +00:00
var (
buf = bytes.NewBuffer(data)
2018-10-13 13:45:53 +00:00
pctx = &parseContext{buf: buf, entries: NewFrameIndex(), staticBase: staticBase}
2017-03-19 22:45:54 +00:00
)
for fn := parselength; buf.Len() != 0; {
fn = fn(pctx)
}
for i := range pctx.entries {
pctx.entries[i].order = order
}
return pctx.entries
}
func cieEntry(data []byte) bool {
return bytes.Equal(data, []byte{0xff, 0xff, 0xff, 0xff})
}
func parselength(ctx *parseContext) parsefunc {
2018-10-13 13:45:53 +00:00
binary.Read(ctx.buf, binary.LittleEndian, &ctx.length)
if ctx.length == 0 {
// ZERO terminator
return parselength
}
var data = ctx.buf.Next(4)
2017-03-19 22:45:54 +00:00
2018-10-13 13:45:53 +00:00
ctx.length -= 4 // take off the length of the CIE id / CIE pointer.
2017-03-19 22:45:54 +00:00
2018-10-13 13:45:53 +00:00
if cieEntry(data) {
ctx.common = &CommonInformationEntry{Length: ctx.length, staticBase: ctx.staticBase}
2017-03-19 22:45:54 +00:00
return parseCIE
}
ctx.frame = &FrameDescriptionEntry{Length: ctx.length, CIE: ctx.common}
return parseFDE
}
func parseFDE(ctx *parseContext) parsefunc {
r := ctx.buf.Next(int(ctx.length))
2018-10-13 13:45:53 +00:00
ctx.frame.begin = binary.LittleEndian.Uint64(r[:8]) + ctx.staticBase
ctx.frame.size = binary.LittleEndian.Uint64(r[8:16])
2017-03-19 22:45:54 +00:00
// Insert into the tree after setting address range begin
// otherwise compares won't work.
ctx.entries = append(ctx.entries, ctx.frame)
// The rest of this entry consists of the instructions
// so we can just grab all of the data from the buffer
// cursor to length.
ctx.frame.Instructions = r[16:]
ctx.length = 0
return parselength
}
func parseCIE(ctx *parseContext) parsefunc {
data := ctx.buf.Next(int(ctx.length))
buf := bytes.NewBuffer(data)
// parse version
ctx.common.Version = data[0]
// parse augmentation
ctx.common.Augmentation, _ = util.ParseString(buf)
// parse code alignment factor
ctx.common.CodeAlignmentFactor, _ = util.DecodeULEB128(buf)
// parse data alignment factor
ctx.common.DataAlignmentFactor, _ = util.DecodeSLEB128(buf)
// parse return address register
ctx.common.ReturnAddressRegister, _ = util.DecodeULEB128(buf)
// parse initial instructions
// The rest of this entry consists of the instructions
// so we can just grab all of the data from the buffer
// cursor to length.
ctx.common.InitialInstructions = buf.Bytes() //ctx.buf.Next(int(ctx.length))
ctx.length = 0
return parselength
}
// DwarfEndian determines the endianness of the DWARF by using the version number field in the debug_info section
// Trick borrowed from "debug/dwarf".New()
func DwarfEndian(infoSec []byte) binary.ByteOrder {
if len(infoSec) < 6 {
return binary.BigEndian
}
x, y := infoSec[4], infoSec[5]
switch {
case x == 0 && y == 0:
return binary.BigEndian
case x == 0:
return binary.BigEndian
case y == 0:
return binary.LittleEndian
default:
return binary.BigEndian
}
}