Skip to content

Commit 0b3a3e9

Browse files
Add compact tables snapshot tests (#1250)
* testdata: Update To include parca-dev/testdata@c0d23d5 Signed-off-by: Francisco Javier Honduvilla Coto <javierhonduco@gmail.com> * .eh_frame: Add snapshot tests for compact unwind table Signed-off-by: Francisco Javier Honduvilla Coto <javierhonduco@gmail.com>
1 parent 582baa5 commit 0b3a3e9

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ $(OUT_BIN_EH_FRAME): go/deps
132132

133133
write-dwarf-unwind-tables: build
134134
make -C testdata validate EH_FRAME_BIN=../dist/eh-frame
135+
make -C testdata validate-compact EH_FRAME_BIN=../dist/eh-frame
135136

136137
test-dwarf-unwind-tables: write-dwarf-unwind-tables
137138
$(CMD_GIT) diff --exit-code testdata/

cmd/eh-frame/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
type flags struct {
2828
Executable string `kong:"help='The executable to print the .eh_unwind tables for.'"`
29+
Compact bool `kong:"help='Whether to use the compact format.'"`
2930
}
3031

3132
// This tool exists for debugging .eh_frame unwinding and its intended for Parca Agent's
@@ -45,7 +46,7 @@ func main() {
4546
}
4647

4748
ptb := unwind.NewUnwindTableBuilder(logger)
48-
err := ptb.PrintTable(os.Stdout, executablePath)
49+
err := ptb.PrintTable(os.Stdout, executablePath, flags.Compact)
4950
if err != nil {
5051
// nolint
5152
fmt.Println("failed with:", err)

pkg/stack/unwind/unwind_table.go

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func x64RegisterToString(reg uint64) string {
182182
}
183183

184184
// PrintTable is a debugging helper that prints the unwinding table to the given io.Writer.
185-
func (ptb *UnwindTableBuilder) PrintTable(writer io.Writer, path string) error {
185+
func (ptb *UnwindTableBuilder) PrintTable(writer io.Writer, path string, compact bool) error {
186186
fdes, err := ptb.readFDEs(path)
187187
if err != nil {
188188
return err
@@ -206,39 +206,55 @@ func (ptb *UnwindTableBuilder) PrintTable(writer io.Writer, path string) error {
206206
if unwindRow == nil {
207207
break
208208
}
209-
//nolint:exhaustive
210-
switch unwindRow.CFA.Rule {
211-
case frame.RuleCFA:
212-
CFAReg := x64RegisterToString(unwindRow.CFA.Reg)
213-
fmt.Fprintf(writer, "\tLoc: %x CFA: $%s=%-4d", unwindRow.Loc, CFAReg, unwindRow.CFA.Offset)
214-
case frame.RuleExpression:
215-
expressionID := ExpressionIdentifier(unwindRow.CFA.Expression)
216-
if expressionID == ExpressionUnknown {
217-
fmt.Fprintf(writer, "\tLoc: %x CFA: exp ", unwindRow.Loc)
218-
} else {
219-
fmt.Fprintf(writer, "\tLoc: %x CFA: exp (plt %d)", unwindRow.Loc, expressionID)
209+
210+
if compact {
211+
compactRow, err := rowToCompactRow(unwindRow)
212+
if err != nil {
213+
return err
220214
}
221-
default:
222-
return multierror.Append(fmt.Errorf("CFA rule is not valid. This should never happen"))
223-
}
224215

225-
// RuleRegister
226-
//nolint:exhaustive
227-
switch unwindRow.RBP.Rule {
228-
case frame.RuleUndefined, frame.RuleUnknown:
229-
fmt.Fprintf(writer, "\tRBP: u")
230-
case frame.RuleRegister:
231-
RBPReg := x64RegisterToString(unwindRow.RBP.Reg)
232-
fmt.Fprintf(writer, "\tRBP: $%s", RBPReg)
233-
case frame.RuleOffset:
234-
fmt.Fprintf(writer, "\tRBP: c%-4d", unwindRow.RBP.Offset)
235-
case frame.RuleExpression:
236-
fmt.Fprintf(writer, "\tRBP: exp")
237-
default:
238-
panic(fmt.Sprintf("Got rule %d for RBP, which wasn't expected", unwindRow.RBP.Rule))
239-
}
216+
fmt.Fprintf(writer, "\t")
217+
fmt.Fprintf(writer, "pc: %x ", compactRow.Pc())
218+
fmt.Fprintf(writer, "cfa_type: %-2d ", compactRow.CfaType())
219+
fmt.Fprintf(writer, "rbp_type: %-2d ", compactRow.RbpType())
220+
fmt.Fprintf(writer, "cfa_offset: %-4d ", compactRow.CfaOffset())
221+
fmt.Fprintf(writer, "rbp_offset: %-4d", compactRow.RbpOffset())
222+
fmt.Fprintf(writer, "\n")
223+
} else {
224+
//nolint:exhaustive
225+
switch unwindRow.CFA.Rule {
226+
case frame.RuleCFA:
227+
CFAReg := x64RegisterToString(unwindRow.CFA.Reg)
228+
fmt.Fprintf(writer, "\tLoc: %x CFA: $%s=%-4d", unwindRow.Loc, CFAReg, unwindRow.CFA.Offset)
229+
case frame.RuleExpression:
230+
expressionID := ExpressionIdentifier(unwindRow.CFA.Expression)
231+
if expressionID == ExpressionUnknown {
232+
fmt.Fprintf(writer, "\tLoc: %x CFA: exp ", unwindRow.Loc)
233+
} else {
234+
fmt.Fprintf(writer, "\tLoc: %x CFA: exp (plt %d)", unwindRow.Loc, expressionID)
235+
}
236+
default:
237+
return multierror.Append(fmt.Errorf("CFA rule is not valid. This should never happen"))
238+
}
240239

241-
fmt.Fprintf(writer, "\n")
240+
// RuleRegister
241+
//nolint:exhaustive
242+
switch unwindRow.RBP.Rule {
243+
case frame.RuleUndefined, frame.RuleUnknown:
244+
fmt.Fprintf(writer, "\tRBP: u")
245+
case frame.RuleRegister:
246+
RBPReg := x64RegisterToString(unwindRow.RBP.Reg)
247+
fmt.Fprintf(writer, "\tRBP: $%s", RBPReg)
248+
case frame.RuleOffset:
249+
fmt.Fprintf(writer, "\tRBP: c%-4d", unwindRow.RBP.Offset)
250+
case frame.RuleExpression:
251+
fmt.Fprintf(writer, "\tRBP: exp")
252+
default:
253+
panic(fmt.Sprintf("Got rule %d for RBP, which wasn't expected", unwindRow.RBP.Rule))
254+
}
255+
256+
fmt.Fprintf(writer, "\n")
257+
}
242258
}
243259
}
244260

0 commit comments

Comments
 (0)