2014-06-25 19:14:29 +00:00
|
|
|
package frame
|
|
|
|
|
2015-01-14 02:37:10 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
)
|
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-10-11 05:52:05 +00:00
|
|
|
func (fde *FrameDescriptionEntry) Cover(addr uint64) bool {
|
2014-10-11 06:05:27 +00:00
|
|
|
if (addr - fde.begin) < fde.end {
|
2014-06-29 16:52:30 +00:00
|
|
|
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
|
|
|
|
Instructions []byte
|
2014-10-11 06:05:27 +00:00
|
|
|
begin, end uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fde *FrameDescriptionEntry) Begin() uint64 {
|
|
|
|
return fde.begin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fde *FrameDescriptionEntry) End() uint64 {
|
|
|
|
return fde.begin + fde.end
|
2014-06-25 19:14:29 +00:00
|
|
|
}
|
|
|
|
|
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-29 22:34:53 +00:00
|
|
|
return frame.cfa.offset + frame.regs[fde.CIE.ReturnAddressRegister].offset
|
2014-06-25 19:14:29 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 00:14:08 +00:00
|
|
|
type FrameDescriptionEntries []*FrameDescriptionEntry
|
2014-08-04 23:02:08 +00:00
|
|
|
|
2015-02-05 00:14:08 +00:00
|
|
|
func NewFrameIndex() FrameDescriptionEntries {
|
|
|
|
return make(FrameDescriptionEntries, 0, 1000)
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 00:14:08 +00:00
|
|
|
func (fdes FrameDescriptionEntries) FDEForPC(pc uint64) (*FrameDescriptionEntry, error) {
|
2015-01-14 02:37:10 +00:00
|
|
|
idx := sort.Search(len(fdes), func(i int) bool {
|
|
|
|
if fdes[i].Cover(pc) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if fdes[i].More(pc) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2014-08-04 23:02:08 +00:00
|
|
|
|
2015-01-14 02:37:10 +00:00
|
|
|
})
|
|
|
|
if idx == len(fdes) {
|
|
|
|
return nil, fmt.Errorf("could not find FDE for PC %#v", pc)
|
2015-02-05 00:14:08 +00:00
|
|
|
}
|
2015-01-14 02:37:10 +00:00
|
|
|
return fdes[idx], nil
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 00:14:08 +00:00
|
|
|
func (frame *FrameDescriptionEntry) Less(pc uint64) bool {
|
|
|
|
return frame.Begin() > pc
|
2014-08-04 23:02:08 +00:00
|
|
|
}
|
2014-06-25 19:14:29 +00:00
|
|
|
|
2015-02-05 00:14:08 +00:00
|
|
|
func (frame *FrameDescriptionEntry) More(pc uint64) bool {
|
|
|
|
return frame.End() < pc
|
2014-06-25 19:14:29 +00:00
|
|
|
}
|