-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathdebug_linux.go
More file actions
196 lines (183 loc) · 5.11 KB
/
debug_linux.go
File metadata and controls
196 lines (183 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//go:build linux
package netlink
import (
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"syscall"
"unsafe"
"github.com/mdlayher/netlink/nlenc"
"golang.org/x/sys/unix"
)
// newDebugger creates a debugger by parsing key=value arguments.
func newDebugger(args []string) *debugger {
d := &debugger{
Log: log.New(os.Stderr, "nl: ", 0),
Level: 1,
Format: "mnl",
}
for _, a := range args {
kv := strings.Split(a, "=")
if len(kv) != 2 {
continue
}
switch kv[0] {
case "level":
level, err := strconv.Atoi(kv[1])
if err != nil {
panicf("netlink: invalid NLDEBUG level: %q", a)
}
d.Level = level
case "format":
d.Format = kv[1]
}
}
return d
}
// debugf prints debugging information at the specified level, if d.Level is high enough to print the message.
func (d *debugger) debugf(level int, format string, v ...interface{}) {
if d.Level < level {
return
}
switch d.Format {
case "mnl":
colorize := true
_, err := unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ)
if err != nil {
colorize = false
}
for _, iface := range v {
if msg, ok := iface.(Message); ok {
nlmsgFprintf(d.Log.Writer(), msg, colorize)
} else {
d.Log.Printf(format, v...)
}
}
default:
d.Log.Printf(format, v...)
}
}
// nlmsgFprintfHeader prints the netlink message header to fd.
func nlmsgFprintfHeader(fd io.Writer, nlh Header) {
fmt.Fprintf(fd, "----------------\t------------------\n")
fmt.Fprintf(fd, "| %010d |\t| message length |\n", nlh.Length)
fmt.Fprintf(fd, "| %05d | %s%s%s%s |\t| type | flags |\n",
nlh.Type,
ternary(nlh.Flags&Request != 0, "R", "-"),
ternary(nlh.Flags&Multi != 0, "M", "-"),
ternary(nlh.Flags&Acknowledge != 0, "A", "-"),
ternary(nlh.Flags&Echo != 0, "E", "-"),
)
fmt.Fprintf(fd, "| %010d |\t| sequence number|\n", nlh.Sequence)
fmt.Fprintf(fd, "| %010d |\t| port ID |\n", nlh.PID)
fmt.Fprintf(fd, "----------------\t------------------\n")
}
// nlmsgFprintf prints a single Message for netlink errors and attributes.
func nlmsgFprintf(fd io.Writer, m Message, colorize bool) {
var hasHeader bool
nlmsgFprintfHeader(fd, m.Header)
switch {
case m.Header.Type == Error:
hasHeader = true
case m.Header.Type == Done && m.Header.Flags&Multi != 0:
if len(m.Data) == 0 {
return
}
default:
// Neither, nothing to do.
}
// Errno occupies 4 bytes.
const endErrno = 4
if len(m.Data) < endErrno {
return
}
c := nlenc.Int32(m.Data[:endErrno])
if c != 0 {
b := m.Data[0:4]
fmt.Fprintf(fd, "| %.2x %.2x %.2x %.2x |\t",
0xff&b[0], 0xff&b[1],
0xff&b[2], 0xff&b[3])
fmt.Fprintf(fd, "| extra header |\n")
}
// Flags indicate an extended acknowledgement. The type/flags combination
// checked above determines the offset where the TLVs occur.
var off int
if hasHeader {
// There is an nlmsghdr preceding the TLVs.
if len(m.Data) < endErrno+nlmsgHeaderLen {
return
}
// The TLVs should be at the offset indicated by the nlmsghdr.length,
// plus the offset where the header began. But make sure the calculated
// offset is still in-bounds.
h := *(*Header)(unsafe.Pointer(&m.Data[endErrno : endErrno+nlmsgHeaderLen][0]))
off = endErrno + int(h.Length)
if len(m.Data) < off {
return
}
} else {
// There is no nlmsghdr preceding the TLVs, parse them directly.
off = endErrno
}
data := m.Data[off:]
for i := 0; i < len(data); {
// Make sure there's at least a header's worth of data to read on each iteration.
if len(data[i:]) < nlaHeaderLen {
break
}
// Extract the length of the attribute.
l := int(nlenc.Uint16(data[i : i+2]))
// extract the type
t := nlenc.Uint16(data[i+2 : i+4])
// print attribute header
if colorize {
fmt.Fprintf(fd, "|\033[1;31m%05d|\033[1;32m%s%s|\033[1;34m%05d\033[0m|\t",
l,
ternary(t&syscall.NLA_F_NESTED != 0, "N", "-"),
ternary(t&syscall.NLA_F_NET_BYTEORDER != 0, "B", "-"),
t&attrTypeMask)
fmt.Fprintf(fd, "|len |flags| type|\n")
} else {
fmt.Fprintf(fd, "|%05d|%s%s|%05d|\t",
l,
ternary(t&syscall.NLA_F_NESTED != 0, "N", "-"),
ternary(t&syscall.NLA_F_NET_BYTEORDER != 0, "B", "-"),
t&attrTypeMask)
fmt.Fprintf(fd, "|len |flags| type|\n")
}
nextAttr := i + nlaAlign(l)
i += nlaHeaderLen
// Ignore zero-length attributes.
if l == 0 {
continue
}
// If nested check the next attribute
if t&syscall.NLA_F_NESTED != 0 {
continue
}
// Print the remaining attributes bytes
for ; i < nextAttr; i += 4 {
fmt.Fprintf(fd, "| %.2x %.2x %.2x %.2x |\t",
0xff&data[i], 0xff&data[i+1],
0xff&data[i+2], 0xff&data[i+3])
fmt.Fprintf(fd, "| data |")
fmt.Fprintf(fd, "\t %s %s %s %s\n",
ternary(strconv.IsPrint(rune(data[i])), string(data[i]), " "),
ternary(strconv.IsPrint(rune(data[i+1])), string(data[i+1]), " "),
ternary(strconv.IsPrint(rune(data[i+2])), string(data[i+2]), " "),
ternary(strconv.IsPrint(rune(data[i+3])), string(data[i+3]), " "),
)
}
}
fmt.Fprintf(fd, "----------------\t------------------\n")
}
// ternary returns iftrue if cond is true, else iffalse.
func ternary(cond bool, iftrue string, iffalse string) string {
if cond {
return iftrue
}
return iffalse
}