2014-06-25 19:14:29 +00:00
|
|
|
package frame
|
|
|
|
|
2015-01-14 02:37:10 +00:00
|
|
|
import (
|
2016-01-24 08:47:22 +00:00
|
|
|
"encoding/binary"
|
2015-01-14 02:37:10 +00:00
|
|
|
"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
|
2018-05-29 15:01:51 +00:00
|
|
|
staticBase uint64
|
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
|
2018-05-29 15:01:51 +00:00
|
|
|
begin, size uint64
|
2016-01-24 08:47:22 +00:00
|
|
|
order binary.ByteOrder
|
2014-10-11 06:05:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 00:30:25 +00:00
|
|
|
// Returns whether or not the given address is within the
|
|
|
|
// bounds of this frame.
|
|
|
|
func (fde *FrameDescriptionEntry) Cover(addr uint64) bool {
|
2018-05-29 15:01:51 +00:00
|
|
|
return (addr - fde.begin) < fde.size
|
2015-03-03 00:30:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 00:06:04 +00:00
|
|
|
// Address of first location for this frame.
|
2014-10-11 06:05:27 +00:00
|
|
|
func (fde *FrameDescriptionEntry) Begin() uint64 {
|
|
|
|
return fde.begin
|
|
|
|
}
|
|
|
|
|
2015-03-03 00:06:04 +00:00
|
|
|
// Address of last location for this frame.
|
2014-10-11 06:05:27 +00:00
|
|
|
func (fde *FrameDescriptionEntry) End() uint64 {
|
2018-05-29 15:01:51 +00:00
|
|
|
return fde.begin + fde.size
|
2014-06-25 19:14:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 00:06:04 +00:00
|
|
|
// Set up frame for the given PC.
|
2014-06-29 16:52:21 +00:00
|
|
|
func (fde *FrameDescriptionEntry) EstablishFrame(pc uint64) *FrameContext {
|
|
|
|
return executeDwarfProgramUntilPC(fde, pc)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
type ErrNoFDEForPC struct {
|
2016-03-18 08:32:17 +00:00
|
|
|
PC uint64
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:08:18 +00:00
|
|
|
func (err *ErrNoFDEForPC) Error() string {
|
2016-03-18 08:32:17 +00:00
|
|
|
return fmt.Sprintf("could not find FDE for PC %#v", err.PC)
|
|
|
|
}
|
|
|
|
|
2015-03-03 00:06:04 +00:00
|
|
|
// Returns the Frame Description Entry for the given PC.
|
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 {
|
2018-05-29 15:01:51 +00:00
|
|
|
return fdes[i].Cover(pc) || fdes[i].Begin() >= pc
|
2015-01-14 02:37:10 +00:00
|
|
|
})
|
2018-05-29 15:01:51 +00:00
|
|
|
if idx == len(fdes) || !fdes[idx].Cover(pc) {
|
2018-08-31 18:08:18 +00:00
|
|
|
return nil, &ErrNoFDEForPC{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
|
|
|
}
|
2019-05-08 21:06:38 +00:00
|
|
|
|
|
|
|
// Append appends otherFDEs to fdes and returns the result.
|
|
|
|
func (fdes FrameDescriptionEntries) Append(otherFDEs FrameDescriptionEntries) FrameDescriptionEntries {
|
|
|
|
r := append(fdes, otherFDEs...)
|
|
|
|
sort.Slice(r, func(i, j int) bool {
|
|
|
|
return r[i].Begin() < r[j].Begin()
|
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|