-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
423 lines (392 loc) · 9.32 KB
/
main.go
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main
import (
"encoding/xml"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/introspect"
)
var (
qFlag bool
systemFlag bool
indentFlag string
noColorFlag bool
sigsFlag bool
valuesFlag bool
stdinFlag bool
methodsFlag bool
propertiesFlag bool
signalsFlag bool
)
var errUsage = errors.New("invalid usage")
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: dbus-inspect [flag...] <destination> [path...]\n\nFlags:\n")
flag.PrintDefaults()
}
flag.BoolVar(&qFlag, "q", false, "provide short overview, destination names or paths only")
flag.BoolVar(&systemFlag, "system", false, "connect to the system bus instead of the session one")
flag.StringVar(&indentFlag, "indent", " ", "set indentation string")
flag.BoolVar(&noColorFlag, "no-color", false, "disable color in output text")
flag.BoolVar(&sigsFlag, "signatures", false, "show argument signatures instead of human-readable types")
flag.BoolVar(&methodsFlag, "methods", false, "show only methods")
flag.BoolVar(&propertiesFlag, "props", false, "show only properties")
flag.BoolVar(&signalsFlag, "signals", false, "show only signals")
flag.BoolVar(&valuesFlag, "values", false, "read property values")
flag.BoolVar(&stdinFlag, "stdin", false, "introspect stdin")
flag.Parse()
if err := run(os.Stdout); err != nil {
if err == errUsage {
flag.Usage()
os.Exit(2)
}
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
func run(w io.Writer) error {
if stdinFlag {
if valuesFlag {
return errors.New("cannot combine -values flag with -stdin")
}
return introspectStdin(w)
}
c, err := connect()
if err != nil {
return err
}
defer c.Close()
if flag.NArg() == 0 {
return listNames(c)
}
if flag.NArg() > 2 {
return errUsage
}
dest := flag.Arg(0)
path := dbus.ObjectPath("/")
recursive := true
if flag.NArg() > 1 {
recursive = false
path = dbus.ObjectPath(flag.Arg(1))
}
if err = introspectNode(w, c, dest, path, recursive); err != nil {
return err
}
return nil
}
func introspectStdin(w io.Writer) error {
var node introspect.Node
if err := xml.NewDecoder(os.Stdin).Decode(&node); err != nil {
return err
}
return printNode(w, &node, nil, indent(0))
}
func connect() (*dbus.Conn, error) {
if systemFlag {
return dbus.SystemBus()
}
return dbus.SessionBus()
}
func introspectNode(
w io.Writer, c *dbus.Conn,
dest string, path dbus.ObjectPath, recursive bool,
) error {
var s string
if err := c.Object(dest, path).Call(
"org.freedesktop.DBus.Introspectable.Introspect", 0,
).Store(&s); err != nil {
return err
}
var node introspect.Node
if err := xml.Unmarshal([]byte(s), &node); err != nil {
return err
}
if qFlag {
fmt.Fprintln(w, string(path))
goto Children
}
fmt.Fprintln(w, color(string(path), termBold))
if err := printNode(w, &node, c.Object(dest, path), indent(1)); err != nil {
return err
}
if !recursive {
return nil
}
Children:
for _, child := range node.Children {
next := dbus.ObjectPath("/" + child.Name)
if path != "/" {
next = path + next
}
if err := introspectNode(w, c, dest, next, recursive); err != nil {
return err
}
}
return nil
}
func sectionEnabled(v bool) bool {
return (!methodsFlag && !propertiesFlag && !signalsFlag) || v
}
func printNode(w io.Writer, node *introspect.Node, o dbus.BusObject, indent func(int) string) error {
for _, iface := range node.Interfaces {
fmt.Fprintln(w, indent(0)+color(iface.Name, termGreen))
if len(iface.Methods) > 0 && sectionEnabled(methodsFlag) {
fmt.Fprintln(w, indent(1)+color("Methods", termYellow))
for _, method := range iface.Methods {
for _, annotation := range method.Annotations {
fmt.Fprintf(w, "%s%s\n", indent(2), fmtAnnotation(annotation))
}
in, out, err := methodArgs(method.Args)
if err != nil {
return err
}
fmt.Fprintf(w, "%s%s(%s) → (%s)\n", indent(2), method.Name, fmtArgs(in), fmtArgs(out))
}
}
if len(iface.Properties) > 0 && sectionEnabled(propertiesFlag) {
fmt.Fprintln(w, indent(1)+color("Properties", termYellow))
for _, property := range iface.Properties {
for _, annotation := range property.Annotations {
fmt.Fprintf(w, "%s%s\n", indent(2), fmtAnnotation(annotation))
}
fmt.Fprintf(w, "%s%s %s %s",
indent(2),
property.Name,
fmtArgType(property.Type),
color("["+property.Access+"]", termGray),
)
// o is nil when we're reading xml definition from a file
if o == nil || !valuesFlag {
fmt.Fprintln(w)
} else {
var v interface{}
if err := o.Call(
"org.freedesktop.DBus.Properties.Get", 0, iface.Name, property.Name,
).Store(&v); err != nil {
v = color("(error: "+err.Error()+")", termRed)
}
fmt.Fprintf(w, " = %v\n", v)
}
}
}
if len(iface.Signals) > 0 && sectionEnabled(signalsFlag) {
fmt.Fprintln(w, indent(1)+color("Signals", termYellow))
for _, signal := range iface.Signals {
for _, annotation := range signal.Annotations {
fmt.Fprintf(w, "%s%s\n", indent(2), fmtAnnotation(annotation))
}
fmt.Fprintf(w, "%s%s(%s)\n", indent(2), signal.Name, fmtArgs(signal.Args))
}
}
}
return nil
}
func indent(n int) func(int) string {
return func(i int) string {
return strings.Repeat(indentFlag, i+n)
}
}
const (
termBold = "1"
termRed = "31"
termGreen = "32"
termYellow = "33"
termBlue = "34"
termMagenta = "35"
termGray = "90"
)
func color(s string, nums ...string) string {
if len(nums) == 0 {
panic("no colors given")
}
if noColorFlag {
return s
}
r := strings.Builder{}
for _, num := range nums {
r.WriteString("\x1b[" + num + "m")
}
r.WriteString(s)
r.WriteString("\x1b[0m")
return r.String()
}
func listNames(c *dbus.Conn) error {
var dests []string
if err := c.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&dests); err != nil {
return err
}
// move down names that start with ':'
sort.Slice(dests, func(i, j int) bool {
if dests[i][0] == ':' && dests[j][0] != ':' {
return false
}
if dests[i][0] != ':' && dests[j][0] == ':' {
return true
}
return dests[i] < dests[j]
})
for _, dest := range dests {
if qFlag {
fmt.Println(dest)
continue
}
var pid uint32
if err := c.BusObject().Call(
"org.freedesktop.DBus.GetConnectionUnixProcessID", 0, dest,
).Store(&pid); err != nil {
return err
}
fmt.Printf("%s %s %s\n",
color(dest, termBold),
color(fmt.Sprintf("%d", pid), termBlue),
color(cmdline(pid), termGray),
)
}
return nil
}
func cmdline(pid uint32) string {
b, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
if err != nil {
return ""
}
cmdline := strings.ReplaceAll(string(b), "\x00", " ")
return cmdline[:len(cmdline)-1]
}
func methodArgs(args []introspect.Arg) ([]introspect.Arg, []introspect.Arg, error) {
var in, out []introspect.Arg
for _, arg := range args {
switch arg.Direction {
case "in":
in = append(in, arg)
case "out":
out = append(out, arg)
default:
return nil, nil, fmt.Errorf("unknown arg direction: %q", arg.Direction)
}
}
return in, out, nil
}
func fmtArgs(args []introspect.Arg) string {
s := make([]string, 0, len(args))
for i, arg := range args {
s = append(s, fmtArg(i, arg))
}
return strings.Join(s, ", ")
}
func fmtArg(i int, arg introspect.Arg) string {
name := arg.Name
if name == "" {
name = "arg_" + strconv.Itoa(i)
}
return color(name, termGray) + " " + fmtArgType(arg.Type)
}
func fmtArgType(s string) string {
if sigsFlag {
return color(s, termBlue)
}
return color(fmtSig(s), termBlue)
}
func fmtAnnotation(annotation introspect.Annotation) string {
return color(fmt.Sprintf("@%s = %s", annotation.Name, annotation.Value), termMagenta)
}
func fmtSig(sig string) string {
var r []string
var l int
for {
s, rlen := next(sig[l:])
if rlen == 0 {
return "Malformed(" + sig + ")"
}
r = append(r, s)
l += rlen
if len(sig) == l {
return strings.Join(r, ", ")
}
}
}
func next(sig string) (string, int) {
if len(sig) == 0 {
return "", 0
}
switch sig[0] {
case 'y':
return "Byte", 1
case 'b':
return "Bool", 1
case 'n':
return "Int16", 1
case 'q':
return "Uint16", 1
case 'i':
return "Int32", 1
case 'u':
return "Uint32", 1
case 'x':
return "Int64", 1
case 't':
return "Uint64", 1
case 'd':
return "Double", 1
case 'h':
return "UnixFD", 1
case 's':
return "String", 1
case 'o':
return "Object", 1
case 'v':
return "Variant", 1
case 'g':
return "Signature", 1
case 'a':
if sig[1] == '{' { // dictionary
i := 4
k, rlen := next(sig[2:])
if rlen != 1 {
// key is not a primitive
return "", 0
}
v, rlen := next(sig[3:])
if rlen == 0 {
// value is not available
return "", 0
}
i += rlen
return "Dict{" + k + ", " + v + "}", i
}
s, rlen := next(sig[1:])
return "Array[" + s + "]", rlen + 1
case '(':
i := 1
n := 1
for i < len(sig) && n != 0 {
if sig[i] == '(' {
n++
} else if sig[i] == ')' {
n--
}
i++
}
return "Struct(" + structFields(sig[1:i-1]) + ")", i
default:
return "Unknown(" + string(sig[0]) + ")", 1
}
}
func structFields(sig string) string {
fields := make([]string, 0, len(sig))
for i := 0; i < len(sig); {
s, rlen := next(sig[i:])
if rlen == 0 {
break
}
i += rlen
fields = append(fields, s)
}
return strings.Join(fields, ", ")
}