Skip to content

Commit 57021d6

Browse files
committed
debug: Adding debugger
Fixes: open-policy-agent#6876 Signed-off-by: Johan Fylling <[email protected]>
1 parent cb42725 commit 57021d6

File tree

11 files changed

+3835
-0
lines changed

11 files changed

+3835
-0
lines changed

Diff for: debug/breakpoint.go

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2024 The OPA Authors. All rights reserved.
2+
// Use of this source code is governed by an Apache2
3+
// license that can be found in the LICENSE file.
4+
5+
package debug
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
11+
"github.com/open-policy-agent/opa/ast/location"
12+
)
13+
14+
type Breakpoint interface {
15+
Id() int
16+
Location() location.Location
17+
}
18+
19+
type breakpoint struct {
20+
id int
21+
location location.Location
22+
}
23+
24+
func (b breakpoint) Id() int {
25+
return b.id
26+
}
27+
28+
func (b breakpoint) Location() location.Location {
29+
return b.location
30+
}
31+
32+
func (b breakpoint) String() string {
33+
return fmt.Sprintf("<%d> %s:%d", b.id, b.location.File, b.location.Row)
34+
}
35+
36+
type breakpointList []breakpoint
37+
38+
func (b breakpointList) String() string {
39+
if b == nil {
40+
return "[]"
41+
}
42+
43+
buf := new(bytes.Buffer)
44+
buf.WriteString("[")
45+
for i, bp := range b {
46+
if i > 0 {
47+
buf.WriteString(", ")
48+
}
49+
_, _ = fmt.Fprintf(buf, "%s:%d", bp.location.File, bp.location.Row)
50+
}
51+
buf.WriteString("]")
52+
return buf.String()
53+
}
54+
55+
type breakpointCollection struct {
56+
breakpoints map[string]breakpointList
57+
idCounter int
58+
}
59+
60+
func newBreakpointCollection() *breakpointCollection {
61+
return &breakpointCollection{
62+
breakpoints: map[string]breakpointList{},
63+
}
64+
}
65+
66+
func (bc *breakpointCollection) newId() int {
67+
bc.idCounter++
68+
return bc.idCounter
69+
}
70+
71+
func (bc *breakpointCollection) add(location location.Location) Breakpoint {
72+
bp := breakpoint{
73+
id: bc.newId(),
74+
location: location,
75+
}
76+
bps := bc.breakpoints[bp.location.File]
77+
bps = append(bps, bp)
78+
bc.breakpoints[bp.location.File] = bps
79+
return bp
80+
}
81+
82+
func (bc *breakpointCollection) allForFilePath(path string) breakpointList {
83+
return bc.breakpoints[path]
84+
}
85+
86+
func (bc *breakpointCollection) clear() {
87+
bc.breakpoints = map[string]breakpointList{}
88+
}
89+
90+
func (bc *breakpointCollection) String() string {
91+
if bc == nil {
92+
return "[]"
93+
}
94+
95+
buf := new(bytes.Buffer)
96+
buf.WriteString("[")
97+
for path, bps := range bc.breakpoints {
98+
for i, bp := range bps {
99+
if i > 0 {
100+
buf.WriteString(", ")
101+
}
102+
_, _ = fmt.Fprintf(buf, "%s:%d\n", path, bp.location.Row)
103+
}
104+
}
105+
buf.WriteString("]")
106+
return buf.String()
107+
}

0 commit comments

Comments
 (0)