Skip to content

Commit

Permalink
stack/unwind: Simplify for loop
Browse files Browse the repository at this point in the history
Signed-off-by: Sumera Priyadarsini <[email protected]>
  • Loading branch information
Sylfrena committed Feb 11, 2024
1 parent 00b7582 commit 156c451
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion internal/dwarf/frame/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func executeDWARFInstruction(ctx *Context) error {

fn, err := lookupFunc(instruction, ctx)
if err != nil {
return fmt.Errorf(" DWARF CFA rule is not valid. This should never happen :%w", err)
return fmt.Errorf("DWARF CFA rule is not valid. This should never happen :%w", err)
}
fn(ctx)

Expand Down
15 changes: 7 additions & 8 deletions pkg/stack/unwind/compact_unwind_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ func BuildCompactUnwindTable(fdes frame.FrameDescriptionEntries, arch elf.Machin
return CompactUnwindTable{}, err
}

var err2 error

for insCtx, err1 := frameContext.Next(); frameContext.HasNext(); insCtx, err2 = frameContext.Next() {
if err1 != nil {
return CompactUnwindTable{}, err1
for {
insCtx, err := frameContext.Next()
if err != nil {
return CompactUnwindTable{}, err
}
if err2 != nil {
return CompactUnwindTable{}, err2

if !frameContext.HasNext() {
break
}

row := unwindTableRow(insCtx)
Expand All @@ -175,7 +175,6 @@ func BuildCompactUnwindTable(fdes frame.FrameDescriptionEntries, arch elf.Machin
}
table = append(table, compactRow)
}

lastFunctionPc = fde.End()
}
// Add a synthetic row at the end of the unwind table. It is fine
Expand Down
28 changes: 16 additions & 12 deletions pkg/stack/unwind/unwind_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ func (ptb *UnwindTableBuilder) PrintTable(writer io.Writer, path string, compact
if err != nil {
return err
}
var err2 error
for insCtx, err1 := frameContext.Next(); frameContext.HasNext(); insCtx, err2 = frameContext.Next() {
if err1 != nil {
return err1

for {
insCtx, err := frameContext.Next()
if err != nil {
return err
}
if err2 != nil {
return err2
if !frameContext.HasNext() {
break
}

unwindRow := unwindTableRow(insCtx)

if unwindRow == nil {
Expand Down Expand Up @@ -241,13 +243,15 @@ func BuildUnwindTable(fdes frame.FrameDescriptionEntries) (UnwindTable, error) {
if err != nil {
return UnwindTable{}, err
}
var err2 error
for insCtx, err1 := frameContext.Next(); frameContext.HasNext(); insCtx, err2 = frameContext.Next() {
if err1 != nil {
return UnwindTable{}, err1

for {
insCtx, err := frameContext.Next()
if err != nil {
return UnwindTable{}, err
}
if err2 != nil {
return UnwindTable{}, err2

if !frameContext.HasNext() {
break
}
table = append(table, *unwindTableRow(insCtx))
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/stack/unwind/unwind_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package unwind

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -81,16 +80,19 @@ func benchmarkParsingDWARFUnwindInformation(b *testing.B, executable string) {
for _, fde := range fdes {
frameContext, err := frame.ExecuteDWARFProgram(fde, unwindContext)
if err != nil {
panic(fmt.Sprintf("unable to evaluate DWARF unwind code. Error: %s", err))
b.Fail()
}
var err2 error
for insCtx, err1 := frameContext.Next(); frameContext.HasNext(); insCtx, err2 = frameContext.Next() {
if err1 != nil {
panic(fmt.Sprintf("unable to evaluate DWARF unwind code. Error: %s", err1))

for {
insCtx, err := frameContext.Next()
if err != nil {
b.Fail()
}
if err2 != nil {
panic(fmt.Sprintf("unable to evaluate DWARF unwind code. Error: %s", err2))

if !frameContext.HasNext() {
break
}

unwindRow := unwindTableRow(insCtx)
if unwindRow.RBP.Rule == frame.RuleUndefined || unwindRow.RBP.Offset == 0 {
// u
Expand Down

0 comments on commit 156c451

Please sign in to comment.