2017-03-19 22:45:54 +00:00
|
|
|
package frame
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
ReturnAddressRegister uint64
|
|
|
|
InitialInstructions []byte
|
2018-10-13 13:45:53 +00:00
|
|
|
staticBase uint64
|
2017-03-19 22:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a Frame Descriptor Entry in the
|
|
|
|
// Dwarf .debug_frame section.
|
|
|
|
type FrameDescriptionEntry struct {
|
|
|
|
Length uint32
|
|
|
|
CIE *CommonInformationEntry
|
|
|
|
Instructions []byte
|
2018-10-13 13:45:53 +00:00
|
|
|
begin, size uint64
|
2017-03-19 22:45:54 +00:00
|
|
|
order binary.ByteOrder
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns whether or not the given address is within the
|
|
|
|
// bounds of this frame.
|
|
|
|
func (fde *FrameDescriptionEntry) Cover(addr uint64) bool {
|
2018-10-13 13:45:53 +00:00
|
|
|
return (addr - fde.begin) < fde.size
|
2017-03-19 22:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Address of first location for this frame.
|
|
|
|
func (fde *FrameDescriptionEntry) Begin() uint64 {
|
|
|
|
return fde.begin
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address of last location for this frame.
|
|
|
|
func (fde *FrameDescriptionEntry) End() uint64 {
|
2018-10-13 13:45:53 +00:00
|
|
|
return fde.begin + fde.size
|
2017-03-19 22:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up frame for the given PC.
|
|
|
|
func (fde *FrameDescriptionEntry) EstablishFrame(pc uint64) *FrameContext {
|
|
|
|
return executeDwarfProgramUntilPC(fde, pc)
|
|
|
|
}
|
|
|
|
|
|
|
|
type FrameDescriptionEntries []*FrameDescriptionEntry
|
|
|
|
|
|
|
|
func NewFrameIndex() FrameDescriptionEntries {
|
|
|
|
return make(FrameDescriptionEntries, 0, 1000)
|
|
|
|
}
|
|
|
|
|
2018-10-13 13:45:53 +00:00
|
|
|
type ErrNoFDEForPC struct {
|
2017-03-19 22:45:54 +00:00
|
|
|
PC uint64
|
|
|
|
}
|
|
|
|
|
2018-10-13 13:45:53 +00:00
|
|
|
func (err *ErrNoFDEForPC) Error() string {
|
2017-03-19 22:45:54 +00:00
|
|
|
return fmt.Sprintf("could not find FDE for PC %#v", err.PC)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the Frame Description Entry for the given PC.
|
|
|
|
func (fdes FrameDescriptionEntries) FDEForPC(pc uint64) (*FrameDescriptionEntry, error) {
|
|
|
|
idx := sort.Search(len(fdes), func(i int) bool {
|
2018-10-13 13:45:53 +00:00
|
|
|
return fdes[i].Cover(pc) || fdes[i].Begin() >= pc
|
2017-03-19 22:45:54 +00:00
|
|
|
})
|
2018-10-13 13:45:53 +00:00
|
|
|
if idx == len(fdes) || !fdes[idx].Cover(pc) {
|
|
|
|
return nil, &ErrNoFDEForPC{pc}
|
2017-03-19 22:45:54 +00:00
|
|
|
}
|
|
|
|
return fdes[idx], nil
|
|
|
|
}
|