Skip to content

Commit 5d42e86

Browse files
committed
wip
Signed-off-by: Kemal Akkoyun <[email protected]>
1 parent 1eb2f5f commit 5d42e86

15 files changed

+578
-63
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ issues:
4545
- path: _test.go
4646
linters:
4747
- errcheck
48+
- gocritic
4849
- path: pkg/elfwriter
4950
linters:
5051
- dupl

internal/dwarf/frame/entries.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// nolint:stylecheck,deadcode,varcheck
12
package frame
23

34
import (
@@ -23,9 +24,9 @@ type CommonInformationEntry struct {
2324
ptrEncAddr ptrEnc
2425
}
2526

26-
// FrameDescriptionEntry represents a Frame Descriptor Entry in the
27+
// DescriptionEntry represents a Frame Descriptor Entry in the
2728
// Dwarf .debug_frame section.
28-
type FrameDescriptionEntry struct {
29+
type DescriptionEntry struct {
2930
Length uint32
3031
CIE *CommonInformationEntry
3132
Instructions []byte
@@ -35,59 +36,59 @@ type FrameDescriptionEntry struct {
3536

3637
// Cover returns whether or not the given address is within the
3738
// bounds of this frame.
38-
func (fde *FrameDescriptionEntry) Cover(addr uint64) bool {
39+
func (fde *DescriptionEntry) Cover(addr uint64) bool {
3940
return (addr - fde.begin) < fde.size
4041
}
4142

4243
// Begin returns address of first location for this frame.
43-
func (fde *FrameDescriptionEntry) Begin() uint64 {
44+
func (fde *DescriptionEntry) Begin() uint64 {
4445
return fde.begin
4546
}
4647

4748
// End returns address of last location for this frame.
48-
func (fde *FrameDescriptionEntry) End() uint64 {
49+
func (fde *DescriptionEntry) End() uint64 {
4950
return fde.begin + fde.size
5051
}
5152

5253
// Translate moves the beginning of fde forward by delta.
53-
func (fde *FrameDescriptionEntry) Translate(delta uint64) {
54+
func (fde *DescriptionEntry) Translate(delta uint64) {
5455
fde.begin += delta
5556
}
5657

5758
// EstablishFrame set up frame for the given PC.
58-
func (fde *FrameDescriptionEntry) EstablishFrame(pc uint64) *FrameContext {
59+
func (fde *DescriptionEntry) EstablishFrame(pc uint64) *FrameContext {
5960
return executeDwarfProgramUntilPC(fde, pc)
6061
}
6162

62-
type FrameDescriptionEntries []*FrameDescriptionEntry
63+
type FrameDescriptionEntries []*DescriptionEntry
6364

6465
func newFrameIndex() FrameDescriptionEntries {
6566
return make(FrameDescriptionEntries, 0, 1000)
6667
}
6768

68-
// ErrNoFDEForPC FDE for PC not found error
69-
type ErrNoFDEForPC struct {
69+
// NoFDEForPCError FDE for PC not found error.
70+
type NoFDEForPCError struct {
7071
PC uint64
7172
}
7273

73-
func (err *ErrNoFDEForPC) Error() string {
74+
func (err *NoFDEForPCError) Error() string {
7475
return fmt.Sprintf("could not find FDE for PC %#v", err.PC)
7576
}
7677

7778
// FDEForPC returns the Frame Description Entry for the given PC.
78-
func (fdes FrameDescriptionEntries) FDEForPC(pc uint64) (*FrameDescriptionEntry, error) {
79+
func (fdes FrameDescriptionEntries) FDEForPC(pc uint64) (*DescriptionEntry, error) {
7980
idx := sort.Search(len(fdes), func(i int) bool {
8081
return fdes[i].Cover(pc) || fdes[i].Begin() >= pc
8182
})
8283
if idx == len(fdes) || !fdes[idx].Cover(pc) {
83-
return nil, &ErrNoFDEForPC{pc}
84+
return nil, &NoFDEForPCError{pc}
8485
}
8586
return fdes[idx], nil
8687
}
8788

8889
// Append appends otherFDEs to fdes and returns the result.
8990
func (fdes FrameDescriptionEntries) Append(otherFDEs FrameDescriptionEntries) FrameDescriptionEntries {
90-
r := append(fdes, otherFDEs...)
91+
r := append(fdes, otherFDEs...) //nolint:gocritic
9192
sort.SliceStable(r, func(i, j int) bool {
9293
return r[i].Begin() < r[j].Begin()
9394
})

internal/dwarf/frame/entries_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package frame
22

33
import (
44
"encoding/binary"
5-
"io/ioutil"
5+
"io"
66
"os"
77
"testing"
88
"unsafe"
@@ -15,14 +15,14 @@ func ptrSizeByRuntimeArch() int {
1515
func TestFDEForPC(t *testing.T) {
1616
frames := newFrameIndex()
1717
frames = append(frames,
18-
&FrameDescriptionEntry{begin: 10, size: 40},
19-
&FrameDescriptionEntry{begin: 50, size: 50},
20-
&FrameDescriptionEntry{begin: 100, size: 100},
21-
&FrameDescriptionEntry{begin: 300, size: 10})
18+
&DescriptionEntry{begin: 10, size: 40},
19+
&DescriptionEntry{begin: 50, size: 50},
20+
&DescriptionEntry{begin: 100, size: 100},
21+
&DescriptionEntry{begin: 300, size: 10})
2222

2323
for _, test := range []struct {
2424
pc uint64
25-
fde *FrameDescriptionEntry
25+
fde *DescriptionEntry
2626
}{
2727
{0, nil},
2828
{9, nil},
@@ -38,8 +38,8 @@ func TestFDEForPC(t *testing.T) {
3838
{300, frames[3]},
3939
{309, frames[3]},
4040
{310, nil},
41-
{400, nil}} {
42-
41+
{400, nil},
42+
} {
4343
out, err := frames.FDEForPC(test.pc)
4444
if test.fde != nil {
4545
if err != nil {
@@ -63,7 +63,7 @@ func BenchmarkFDEForPC(b *testing.B) {
6363
}
6464
defer f.Close()
6565

66-
data, err := ioutil.ReadAll(f)
66+
data, err := io.ReadAll(f)
6767
if err != nil {
6868
b.Fatal(err)
6969
}

internal/dwarf/frame/expression_constants.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
// nolint:stylecheck
12
package frame
23

3-
// Operation opcodes
4+
// Operation opcodes.
45
const (
56
DW_OP_addr = 0x03
67
DW_OP_const1s = 0x09

internal/dwarf/frame/parser.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"fmt"
1010
"io"
1111

12-
"github.com/go-delve/delve/pkg/dwarf/util"
12+
"github.com/parca-dev/parca-agent/internal/dwarf/util"
1313
)
1414

15+
// TODO(kakkoyun): Can we speed parsin using or look up using .eh_frame_hdr?
16+
1517
type parsefunc func(*parseContext) parsefunc
1618

1719
type parseContext struct {
@@ -22,18 +24,18 @@ type parseContext struct {
2224
entries FrameDescriptionEntries
2325
ciemap map[int]*CommonInformationEntry
2426
common *CommonInformationEntry
25-
frame *FrameDescriptionEntry
27+
frame *DescriptionEntry
2628
length uint32
2729
ptrSize int
2830
ehFrameAddr uint64
2931
err error
3032
}
3133

3234
// Parse takes in data (a byte slice) and returns FrameDescriptionEntries,
33-
// which is a slice of FrameDescriptionEntry. Each FrameDescriptionEntry
35+
// which is a slice of DescriptionEntry. Each DescriptionEntry
3436
// has a pointer to CommonInformationEntry.
3537
// If ehFrameAddr is not zero the .eh_frame format will be used, a minor variant of DWARF described at https://www.airs.com/blog/archives/460.
36-
// The value of ehFrameAddr will be used as the address at which eh_frame will be mapped into memory
38+
// The value of ehFrameAddr will be used as the address at which eh_frame will be mapped into memory.
3739
func Parse(data []byte, order binary.ByteOrder, staticBase uint64, ptrSize int, ehFrameAddr uint64) (FrameDescriptionEntries, error) {
3840
var (
3941
buf = bytes.NewBuffer(data)
@@ -71,15 +73,15 @@ func (ctx *parseContext) offset() int {
7173

7274
func parselength(ctx *parseContext) parsefunc {
7375
start := ctx.offset()
74-
binary.Read(ctx.buf, binary.LittleEndian, &ctx.length) // TODO(aarzilli): this does not support 64bit DWARF
76+
_ = binary.Read(ctx.buf, binary.LittleEndian, &ctx.length) // TODO(aarzilli): this does not support 64bit DWARF
7577

7678
if ctx.length == 0 {
7779
// ZERO terminator
7880
return parselength
7981
}
8082

8183
var cieid uint32
82-
binary.Read(ctx.buf, binary.LittleEndian, &cieid)
84+
_ = binary.Read(ctx.buf, binary.LittleEndian, &cieid)
8385

8486
ctx.length -= 4 // take off the length of the CIE id / CIE pointer.
8587

@@ -99,7 +101,7 @@ func parselength(ctx *parseContext) parsefunc {
99101
ctx.err = fmt.Errorf("unknown CIE_id %#x at %#x", cieid, start)
100102
}
101103

102-
ctx.frame = &FrameDescriptionEntry{Length: ctx.length, CIE: common}
104+
ctx.frame = &DescriptionEntry{Length: ctx.length, CIE: common}
103105
return parseFDE
104106
}
105107

@@ -127,7 +129,7 @@ func parseFDE(ctx *parseContext) parsefunc {
127129
// need to read the augmentation data, which are encoded as a ULEB128
128130
// size followed by 'size' bytes.
129131
n, _ := util.DecodeULEB128(reader)
130-
reader.Seek(int64(n), io.SeekCurrent)
132+
_, _ = reader.Seek(int64(n), io.SeekCurrent)
131133
}
132134

133135
// The rest of this entry consists of the instructions
@@ -238,6 +240,7 @@ func (ctx *parseContext) readEncodedPtr(addr uint64, buf util.ByteReaderWithLen,
238240

239241
var ptr uint64
240242

243+
//nolint:exhaustive
241244
switch ptrEnc & 0xf {
242245
case ptrEncAbs, ptrEncSigned:
243246
ptr, _ = util.ReadUintRaw(buf, binary.LittleEndian, ctx.ptrSize)
@@ -268,7 +271,7 @@ func (ctx *parseContext) readEncodedPtr(addr uint64, buf util.ByteReaderWithLen,
268271
}
269272

270273
// DwarfEndian determines the endianness of the DWARF by using the version number field in the debug_info section
271-
// Trick borrowed from "debug/dwarf".New()
274+
// Trick borrowed from "debug/dwarf".New().
272275
func DwarfEndian(infoSec []byte) binary.ByteOrder {
273276
if len(infoSec) < 6 {
274277
return binary.BigEndian

internal/dwarf/frame/parser_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"debug/elf"
66
"encoding/binary"
7-
"io/ioutil"
7+
"io"
88
"os"
99
"testing"
1010
)
@@ -110,7 +110,7 @@ func TestParse(t *testing.T) {
110110
}
111111

112112
var ehFrameAddr uint64 = 0
113-
var byteOrder = obj.ByteOrder
113+
byteOrder := obj.ByteOrder
114114
if tt.args.sectionName == ".eh_frame" {
115115
ehFrameAddr = sec.Addr
116116
} else {
@@ -137,7 +137,7 @@ func BenchmarkParse(b *testing.B) {
137137
}
138138
defer f.Close()
139139

140-
data, err := ioutil.ReadAll(f)
140+
data, err := io.ReadAll(f)
141141
if err != nil {
142142
b.Fatal(err)
143143
}

internal/dwarf/frame/table.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
// nolint:stylecheck,deadcode
12
package frame
23

34
import (
45
"bytes"
56
"encoding/binary"
67
"fmt"
78

8-
"github.com/go-delve/delve/pkg/dwarf/util"
9+
"github.com/parca-dev/parca-agent/internal/dwarf/util"
910
)
1011

1112
// DWRule wrapper of rule defined for register values.
@@ -16,7 +17,7 @@ type DWRule struct {
1617
Expression []byte
1718
}
1819

19-
// FrameContext wrapper of FDE context
20+
// FrameContext wrapper of FDE context.
2021
type FrameContext struct {
2122
loc uint64
2223
order binary.ByteOrder
@@ -141,7 +142,7 @@ func executeCIEInstructions(cie *CommonInformationEntry) *FrameContext {
141142
}
142143

143144
// Unwind the stack to find the return address register.
144-
func executeDwarfProgramUntilPC(fde *FrameDescriptionEntry, pc uint64) *FrameContext {
145+
func executeDwarfProgramUntilPC(fde *DescriptionEntry, pc uint64) *FrameContext {
145146
frame := executeCIEInstructions(fde.CIE)
146147
frame.order = fde.order
147148
frame.loc = fde.Begin()
@@ -152,7 +153,7 @@ func executeDwarfProgramUntilPC(fde *FrameDescriptionEntry, pc uint64) *FrameCon
152153
}
153154

154155
// ExecuteDwarfProgram unwinds the stack to find the return address register.
155-
func ExecuteDwarfProgram(fde *FrameDescriptionEntry) *FrameContext {
156+
func ExecuteDwarfProgram(fde *DescriptionEntry) *FrameContext {
156157
frame := executeCIEInstructions(fde.CIE)
157158
frame.order = fde.order
158159
frame.loc = fde.Begin()
@@ -275,14 +276,14 @@ func advanceloc1(frame *FrameContext) {
275276

276277
func advanceloc2(frame *FrameContext) {
277278
var delta uint16
278-
binary.Read(frame.buf, frame.order, &delta)
279+
_ = binary.Read(frame.buf, frame.order, &delta)
279280

280281
frame.loc += uint64(delta) * frame.codeAlignment
281282
}
282283

283284
func advanceloc4(frame *FrameContext) {
284285
var delta uint32
285-
binary.Read(frame.buf, frame.order, &delta)
286+
_ = binary.Read(frame.buf, frame.order, &delta)
286287

287288
frame.loc += uint64(delta) * frame.codeAlignment
288289
}
@@ -318,7 +319,7 @@ func restore(frame *FrameContext) {
318319

319320
func setloc(frame *FrameContext) {
320321
var loc uint64
321-
binary.Read(frame.buf, frame.order, &loc)
322+
_ = binary.Read(frame.buf, frame.order, &loc)
322323

323324
frame.loc = loc + frame.cie.staticBase
324325
}
@@ -475,8 +476,7 @@ func gnuargsize(frame *FrameContext) {
475476

476477
// TODO(kakkoyun): ? How to move cursor without corrupting? Do we actually need to do this?
477478
func unknown(frame *FrameContext) {
478-
_, err := frame.buf.ReadByte()
479-
if err != nil {
479+
if _, err := frame.buf.ReadByte(); err != nil {
480480
panic("Could not read byte")
481481
}
482482
}

0 commit comments

Comments
 (0)