2014-06-25 19:14:29 +00:00
|
|
|
package frame
|
|
|
|
|
2014-08-07 16:03:42 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/derekparker/rbtree"
|
|
|
|
)
|
2014-06-25 19:14:29 +00:00
|
|
|
|
|
|
|
// Represents a Common Information Entry in
|
|
|
|
// the Dwarf .debug_frame section.
|
|
|
|
type CommonInformationEntry struct {
|
|
|
|
Length uint32
|
|
|
|
CIE_id uint32
|
|
|
|
Version uint8
|
|
|
|
Augmentation string
|
|
|
|
CodeAlignmentFactor uint64
|
|
|
|
DataAlignmentFactor int64
|
2014-07-29 22:34:53 +00:00
|
|
|
ReturnAddressRegister uint64
|
2014-06-25 19:14:29 +00:00
|
|
|
InitialInstructions []byte
|
|
|
|
}
|
|
|
|
|
2014-06-29 16:52:30 +00:00
|
|
|
type addrange struct {
|
|
|
|
begin, end uint64
|
|
|
|
}
|
|
|
|
|
2014-08-04 23:02:08 +00:00
|
|
|
func (r *addrange) Begin() uint64 {
|
|
|
|
return r.begin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *addrange) End() uint64 {
|
|
|
|
return r.end
|
|
|
|
}
|
|
|
|
|
2014-06-29 16:52:30 +00:00
|
|
|
func (r *addrange) Cover(addr uint64) bool {
|
|
|
|
if (addr - r.begin) < r.end {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2014-06-25 19:14:29 +00:00
|
|
|
|
|
|
|
// Represents a Frame Descriptor Entry in the
|
|
|
|
// Dwarf .debug_frame section.
|
|
|
|
type FrameDescriptionEntry struct {
|
|
|
|
Length uint32
|
|
|
|
CIE *CommonInformationEntry
|
|
|
|
AddressRange *addrange
|
|
|
|
Instructions []byte
|
|
|
|
}
|
|
|
|
|
2014-06-29 16:52:21 +00:00
|
|
|
func (fde *FrameDescriptionEntry) EstablishFrame(pc uint64) *FrameContext {
|
|
|
|
return executeDwarfProgramUntilPC(fde, pc)
|
|
|
|
}
|
|
|
|
|
2014-06-29 16:52:30 +00:00
|
|
|
func (fde *FrameDescriptionEntry) ReturnAddressOffset(pc uint64) int64 {
|
2014-06-29 16:52:21 +00:00
|
|
|
frame := fde.EstablishFrame(pc)
|
2014-07-10 23:07:39 +00:00
|
|
|
|
2014-07-29 22:34:53 +00:00
|
|
|
return frame.cfa.offset + frame.regs[fde.CIE.ReturnAddressRegister].offset
|
2014-06-25 19:14:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 23:02:08 +00:00
|
|
|
type FrameDescriptionEntries struct {
|
2014-08-07 16:03:42 +00:00
|
|
|
*rbtree.RedBlackTree
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFrameIndex() *FrameDescriptionEntries {
|
2014-08-07 16:03:42 +00:00
|
|
|
return &FrameDescriptionEntries{rbtree.New()}
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 16:03:42 +00:00
|
|
|
func (fdes *FrameDescriptionEntries) FDEForPC(pc uint64) (*FrameDescriptionEntry, error) {
|
|
|
|
node, ok := fdes.Find(Addr(pc))
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Could not find FDE for %#v", pc)
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 16:03:42 +00:00
|
|
|
return node.(*FrameDescriptionEntry), nil
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 16:03:42 +00:00
|
|
|
func (frame *FrameDescriptionEntry) Less(item rbtree.Item) bool {
|
|
|
|
return frame.AddressRange.begin < item.(*FrameDescriptionEntry).AddressRange.begin
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 16:03:42 +00:00
|
|
|
func (frame *FrameDescriptionEntry) More(item rbtree.Item) bool {
|
|
|
|
r := item.(*FrameDescriptionEntry).AddressRange
|
|
|
|
fnr := frame.AddressRange
|
|
|
|
return fnr.begin+fnr.end > r.begin+r.end
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 16:03:42 +00:00
|
|
|
type Addr uint64
|
2014-08-04 23:02:08 +00:00
|
|
|
|
2014-08-07 16:03:42 +00:00
|
|
|
func (a Addr) Less(item rbtree.Item) bool {
|
|
|
|
return uint64(a) < item.(*FrameDescriptionEntry).AddressRange.begin
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
2014-06-25 19:14:29 +00:00
|
|
|
|
2014-08-07 16:03:42 +00:00
|
|
|
func (a Addr) More(item rbtree.Item) bool {
|
|
|
|
r := item.(*FrameDescriptionEntry).AddressRange
|
|
|
|
return uint64(a) > r.begin+r.end
|
2014-06-25 19:14:29 +00:00
|
|
|
}
|