summaryrefslogtreecommitdiffhomepage
path: root/pkg/state/printer.go
blob: c61ec4a2622f75e2fd609f7b0d27d2e4d6b2caee (plain)
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
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package state

import (
	"fmt"
	"io"
	"io/ioutil"
	"strings"

	"github.com/golang/protobuf/proto"
	pb "gvisor.googlesource.com/gvisor/pkg/state/object_go_proto"
)

// format formats a single object, for pretty-printing.
func format(graph uint64, depth int, object *pb.Object, html bool) (string, bool) {
	switch x := object.GetValue().(type) {
	case *pb.Object_BoolValue:
		return fmt.Sprintf("%t", x.BoolValue), x.BoolValue != false
	case *pb.Object_StringValue:
		return fmt.Sprintf("\"%s\"", x.StringValue), x.StringValue != ""
	case *pb.Object_Int64Value:
		return fmt.Sprintf("%d", x.Int64Value), x.Int64Value != 0
	case *pb.Object_Uint64Value:
		return fmt.Sprintf("%du", x.Uint64Value), x.Uint64Value != 0
	case *pb.Object_DoubleValue:
		return fmt.Sprintf("%f", x.DoubleValue), x.DoubleValue != 0.0
	case *pb.Object_RefValue:
		if x.RefValue == 0 {
			return "nil", false
		}
		ref := fmt.Sprintf("g%dr%d", graph, x.RefValue)
		if html {
			ref = fmt.Sprintf("<a href=#%s>%s</a>", ref, ref)
		}
		return ref, true
	case *pb.Object_SliceValue:
		if x.SliceValue.RefValue == 0 {
			return "nil", false
		}
		ref := fmt.Sprintf("g%dr%d", graph, x.SliceValue.RefValue)
		if html {
			ref = fmt.Sprintf("<a href=#%s>%s</a>", ref, ref)
		}
		return fmt.Sprintf("%s[:%d:%d]", ref, x.SliceValue.Length, x.SliceValue.Capacity), true
	case *pb.Object_ArrayValue:
		if len(x.ArrayValue.Contents) == 0 {
			return "[]", false
		}
		items := make([]string, 0, len(x.ArrayValue.Contents)+2)
		zeros := make([]string, 0) // used to eliminate zero entries.
		items = append(items, "[")
		tabs := "\n" + strings.Repeat("\t", depth)
		for i := 0; i < len(x.ArrayValue.Contents); i++ {
			item, ok := format(graph, depth+1, x.ArrayValue.Contents[i], html)
			if ok {
				if len(zeros) > 0 {
					items = append(items, zeros...)
					zeros = nil
				}
				items = append(items, fmt.Sprintf("\t%s,", item))
			} else {
				zeros = append(zeros, fmt.Sprintf("\t%s,", item))
			}
		}
		if len(zeros) > 0 {
			items = append(items, fmt.Sprintf("\t... (%d zero),", len(zeros)))
		}
		items = append(items, "]")
		return strings.Join(items, tabs), len(zeros) < len(x.ArrayValue.Contents)
	case *pb.Object_StructValue:
		if len(x.StructValue.Fields) == 0 {
			return "struct{}", false
		}
		items := make([]string, 0, len(x.StructValue.Fields)+2)
		items = append(items, "struct{")
		tabs := "\n" + strings.Repeat("\t", depth)
		allZero := true
		for _, field := range x.StructValue.Fields {
			element, ok := format(graph, depth+1, field.Value, html)
			allZero = allZero && !ok
			items = append(items, fmt.Sprintf("\t%s: %s,", field.Name, element))
		}
		items = append(items, "}")
		return strings.Join(items, tabs), !allZero
	case *pb.Object_MapValue:
		if len(x.MapValue.Keys) == 0 {
			return "map{}", false
		}
		items := make([]string, 0, len(x.MapValue.Keys)+2)
		items = append(items, "map{")
		tabs := "\n" + strings.Repeat("\t", depth)
		for i := 0; i < len(x.MapValue.Keys); i++ {
			key, _ := format(graph, depth+1, x.MapValue.Keys[i], html)
			value, _ := format(graph, depth+1, x.MapValue.Values[i], html)
			items = append(items, fmt.Sprintf("\t%s: %s,", key, value))
		}
		items = append(items, "}")
		return strings.Join(items, tabs), true
	case *pb.Object_InterfaceValue:
		if x.InterfaceValue.Type == "" {
			return "interface(nil){}", false
		}
		element, _ := format(graph, depth+1, x.InterfaceValue.Value, html)
		return fmt.Sprintf("interface(\"%s\"){%s}", x.InterfaceValue.Type, element), true
	}

	// Should not happen, but tolerate.
	return fmt.Sprintf("(unknown proto type: %T)", object.GetValue()), true
}

// PrettyPrint reads the state stream from r, and pretty prints to w.
func PrettyPrint(w io.Writer, r io.Reader, html bool) error {
	var (
		// current graph ID.
		graph uint64

		// current object ID.
		id uint64
	)

	if html {
		fmt.Fprintf(w, "<pre>")
		defer fmt.Fprintf(w, "</pre>")
	}

	for {
		// Find the first object to begin generation.
		length, object, err := ReadHeader(r)
		if err == io.EOF {
			// Nothing else to do.
			break
		} else if err != nil {
			return err
		}
		if !object {
			// Increment the graph number & reset the ID.
			graph++
			id = 0
			if length > 0 {
				fmt.Fprintf(w, "(%d bytes non-object data)\n", length)
				io.Copy(ioutil.Discard, &io.LimitedReader{
					R: r,
					N: int64(length),
				})
			}
			continue
		}

		// Read & unmarshal the object.
		buf := make([]byte, length)
		for done := 0; done < len(buf); {
			n, err := r.Read(buf[done:])
			done += n
			if n == 0 && err != nil {
				return err
			}
		}
		obj := new(pb.Object)
		if err := proto.Unmarshal(buf, obj); err != nil {
			return err
		}

		id++ // First object must be one.
		str, _ := format(graph, 0, obj, html)
		tag := fmt.Sprintf("g%dr%d", graph, id)
		if html {
			tag = fmt.Sprintf("<a name=%s>%s</a>", tag, tag)
		}
		if _, err := fmt.Fprintf(w, "%s = %s\n", tag, str); err != nil {
			return err
		}
	}

	return nil
}