1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
// Delete the next line to include in the gob package.
|
6 |
|
|
// +build gob-debug
|
7 |
|
|
|
8 |
|
|
package gob
|
9 |
|
|
|
10 |
|
|
// This file is not normally included in the gob package. Used only for debugging the package itself.
|
11 |
|
|
// Add debug.go to the files listed in the Makefile to add Debug to the gob package.
|
12 |
|
|
// Except for reading uints, it is an implementation of a reader that is independent of
|
13 |
|
|
// the one implemented by Decoder.
|
14 |
|
|
|
15 |
|
|
import (
|
16 |
|
|
"bytes"
|
17 |
|
|
"fmt"
|
18 |
|
|
"io"
|
19 |
|
|
"os"
|
20 |
|
|
"strings"
|
21 |
|
|
"sync"
|
22 |
|
|
)
|
23 |
|
|
|
24 |
|
|
var dumpBytes = false // If true, print the remaining bytes in the input buffer at each item.
|
25 |
|
|
|
26 |
|
|
// Init installs the debugging facility. If this file is not compiled in the
|
27 |
|
|
// package, the tests in codec_test.go are no-ops.
|
28 |
|
|
func init() {
|
29 |
|
|
debugFunc = Debug
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
var (
|
33 |
|
|
blanks = bytes.Repeat([]byte{' '}, 3*10)
|
34 |
|
|
empty = []byte(": \n")
|
35 |
|
|
tabs = strings.Repeat("\t", 100)
|
36 |
|
|
)
|
37 |
|
|
|
38 |
|
|
// tab indents itself when printed.
|
39 |
|
|
type tab int
|
40 |
|
|
|
41 |
|
|
func (t tab) String() string {
|
42 |
|
|
n := int(t)
|
43 |
|
|
if n > len(tabs) {
|
44 |
|
|
n = len(tabs)
|
45 |
|
|
}
|
46 |
|
|
return tabs[0:n]
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
func (t tab) print() {
|
50 |
|
|
fmt.Fprint(os.Stderr, t)
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
// A peekReader wraps an io.Reader, allowing one to peek ahead to see
|
54 |
|
|
// what's coming without stealing the data from the client of the Reader.
|
55 |
|
|
type peekReader struct {
|
56 |
|
|
r io.Reader
|
57 |
|
|
data []byte // read-ahead data
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
// newPeekReader returns a peekReader that wraps r.
|
61 |
|
|
func newPeekReader(r io.Reader) *peekReader {
|
62 |
|
|
return &peekReader{r: r}
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
// Read is the usual method. It will first take data that has been read ahead.
|
66 |
|
|
func (p *peekReader) Read(b []byte) (n int, err error) {
|
67 |
|
|
if len(p.data) == 0 {
|
68 |
|
|
return p.r.Read(b)
|
69 |
|
|
}
|
70 |
|
|
// Satisfy what's possible from the read-ahead data.
|
71 |
|
|
n = copy(b, p.data)
|
72 |
|
|
// Move data down to beginning of slice, to avoid endless growth
|
73 |
|
|
copy(p.data, p.data[n:])
|
74 |
|
|
p.data = p.data[:len(p.data)-n]
|
75 |
|
|
return
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
// peek returns as many bytes as possible from the unread
|
79 |
|
|
// portion of the stream, up to the length of b.
|
80 |
|
|
func (p *peekReader) peek(b []byte) (n int, err error) {
|
81 |
|
|
if len(p.data) > 0 {
|
82 |
|
|
n = copy(b, p.data)
|
83 |
|
|
if n == len(b) {
|
84 |
|
|
return
|
85 |
|
|
}
|
86 |
|
|
b = b[n:]
|
87 |
|
|
}
|
88 |
|
|
if len(b) == 0 {
|
89 |
|
|
return
|
90 |
|
|
}
|
91 |
|
|
m, e := io.ReadFull(p.r, b)
|
92 |
|
|
if m > 0 {
|
93 |
|
|
p.data = append(p.data, b[:m]...)
|
94 |
|
|
}
|
95 |
|
|
n += m
|
96 |
|
|
if e == io.ErrUnexpectedEOF {
|
97 |
|
|
// That means m > 0 but we reached EOF. If we got data
|
98 |
|
|
// we won't complain about not being able to peek enough.
|
99 |
|
|
if n > 0 {
|
100 |
|
|
e = nil
|
101 |
|
|
} else {
|
102 |
|
|
e = io.EOF
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
return n, e
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
type debugger struct {
|
109 |
|
|
mutex sync.Mutex
|
110 |
|
|
remain int // the number of bytes known to remain in the input
|
111 |
|
|
remainingKnown bool // the value of 'remain' is valid
|
112 |
|
|
r *peekReader
|
113 |
|
|
wireType map[typeId]*wireType
|
114 |
|
|
tmp []byte // scratch space for decoding uints.
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
// dump prints the next nBytes of the input.
|
118 |
|
|
// It arranges to print the output aligned from call to
|
119 |
|
|
// call, to make it easy to see what has been consumed.
|
120 |
|
|
func (deb *debugger) dump(format string, args ...interface{}) {
|
121 |
|
|
if !dumpBytes {
|
122 |
|
|
return
|
123 |
|
|
}
|
124 |
|
|
fmt.Fprintf(os.Stderr, format+" ", args...)
|
125 |
|
|
if !deb.remainingKnown {
|
126 |
|
|
return
|
127 |
|
|
}
|
128 |
|
|
if deb.remain < 0 {
|
129 |
|
|
fmt.Fprintf(os.Stderr, "remaining byte count is negative! %d\n", deb.remain)
|
130 |
|
|
return
|
131 |
|
|
}
|
132 |
|
|
data := make([]byte, deb.remain)
|
133 |
|
|
n, _ := deb.r.peek(data)
|
134 |
|
|
if n == 0 {
|
135 |
|
|
os.Stderr.Write(empty)
|
136 |
|
|
return
|
137 |
|
|
}
|
138 |
|
|
b := new(bytes.Buffer)
|
139 |
|
|
fmt.Fprintf(b, "[%d]{\n", deb.remain)
|
140 |
|
|
// Blanks until first byte
|
141 |
|
|
lineLength := 0
|
142 |
|
|
if n := len(data); n%10 != 0 {
|
143 |
|
|
lineLength = 10 - n%10
|
144 |
|
|
fmt.Fprintf(b, "\t%s", blanks[:lineLength*3])
|
145 |
|
|
}
|
146 |
|
|
// 10 bytes per line
|
147 |
|
|
for len(data) > 0 {
|
148 |
|
|
if lineLength == 0 {
|
149 |
|
|
fmt.Fprint(b, "\t")
|
150 |
|
|
}
|
151 |
|
|
m := 10 - lineLength
|
152 |
|
|
lineLength = 0
|
153 |
|
|
if m > len(data) {
|
154 |
|
|
m = len(data)
|
155 |
|
|
}
|
156 |
|
|
fmt.Fprintf(b, "% x\n", data[:m])
|
157 |
|
|
data = data[m:]
|
158 |
|
|
}
|
159 |
|
|
fmt.Fprint(b, "}\n")
|
160 |
|
|
os.Stderr.Write(b.Bytes())
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
// Debug prints a human-readable representation of the gob data read from r.
|
164 |
|
|
// It is a no-op unless debugging was enabled when the package was built.
|
165 |
|
|
func Debug(r io.Reader) {
|
166 |
|
|
err := debug(r)
|
167 |
|
|
if err != nil {
|
168 |
|
|
fmt.Fprintf(os.Stderr, "gob debug: %s\n", err)
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
// debug implements Debug, but catches panics and returns
|
173 |
|
|
// them as errors to be printed by Debug.
|
174 |
|
|
func debug(r io.Reader) (err error) {
|
175 |
|
|
defer catchError(&err)
|
176 |
|
|
fmt.Fprintln(os.Stderr, "Start of debugging")
|
177 |
|
|
deb := &debugger{
|
178 |
|
|
r: newPeekReader(r),
|
179 |
|
|
wireType: make(map[typeId]*wireType),
|
180 |
|
|
tmp: make([]byte, 16),
|
181 |
|
|
}
|
182 |
|
|
if b, ok := r.(*bytes.Buffer); ok {
|
183 |
|
|
deb.remain = b.Len()
|
184 |
|
|
deb.remainingKnown = true
|
185 |
|
|
}
|
186 |
|
|
deb.gobStream()
|
187 |
|
|
return
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
// note that we've consumed some bytes
|
191 |
|
|
func (deb *debugger) consumed(n int) {
|
192 |
|
|
if deb.remainingKnown {
|
193 |
|
|
deb.remain -= n
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
// int64 decodes and returns the next integer, which must be present.
|
198 |
|
|
// Don't call this if you could be at EOF.
|
199 |
|
|
func (deb *debugger) int64() int64 {
|
200 |
|
|
return toInt(deb.uint64())
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
// uint64 returns and decodes the next unsigned integer, which must be present.
|
204 |
|
|
// Don't call this if you could be at EOF.
|
205 |
|
|
// TODO: handle errors better.
|
206 |
|
|
func (deb *debugger) uint64() uint64 {
|
207 |
|
|
n, w, err := decodeUintReader(deb.r, deb.tmp)
|
208 |
|
|
if err != nil {
|
209 |
|
|
errorf("debug: read error: %s", err)
|
210 |
|
|
}
|
211 |
|
|
deb.consumed(w)
|
212 |
|
|
return n
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
// GobStream:
|
216 |
|
|
// DelimitedMessage* (until EOF)
|
217 |
|
|
func (deb *debugger) gobStream() {
|
218 |
|
|
// Make sure we're single-threaded through here.
|
219 |
|
|
deb.mutex.Lock()
|
220 |
|
|
defer deb.mutex.Unlock()
|
221 |
|
|
|
222 |
|
|
for deb.delimitedMessage(0) {
|
223 |
|
|
}
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
// DelimitedMessage:
|
227 |
|
|
// uint(lengthOfMessage) Message
|
228 |
|
|
func (deb *debugger) delimitedMessage(indent tab) bool {
|
229 |
|
|
for {
|
230 |
|
|
n := deb.loadBlock(true)
|
231 |
|
|
if n < 0 {
|
232 |
|
|
return false
|
233 |
|
|
}
|
234 |
|
|
deb.dump("Delimited message of length %d", n)
|
235 |
|
|
deb.message(indent)
|
236 |
|
|
}
|
237 |
|
|
return true
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
// loadBlock preps us to read a message
|
241 |
|
|
// of the length specified next in the input. It returns
|
242 |
|
|
// the length of the block. The argument tells whether
|
243 |
|
|
// an EOF is acceptable now. If it is and one is found,
|
244 |
|
|
// the return value is negative.
|
245 |
|
|
func (deb *debugger) loadBlock(eofOK bool) int {
|
246 |
|
|
n64, w, err := decodeUintReader(deb.r, deb.tmp) // deb.uint64 will error at EOF
|
247 |
|
|
if err != nil {
|
248 |
|
|
if eofOK && err == io.EOF {
|
249 |
|
|
return -1
|
250 |
|
|
}
|
251 |
|
|
errorf("debug: unexpected error: %s", err)
|
252 |
|
|
}
|
253 |
|
|
deb.consumed(w)
|
254 |
|
|
n := int(n64)
|
255 |
|
|
if n < 0 {
|
256 |
|
|
errorf("huge value for message length: %d", n64)
|
257 |
|
|
}
|
258 |
|
|
return int(n)
|
259 |
|
|
}
|
260 |
|
|
|
261 |
|
|
// Message:
|
262 |
|
|
// TypeSequence TypedValue
|
263 |
|
|
// TypeSequence
|
264 |
|
|
// (TypeDefinition DelimitedTypeDefinition*)?
|
265 |
|
|
// DelimitedTypeDefinition:
|
266 |
|
|
// uint(lengthOfTypeDefinition) TypeDefinition
|
267 |
|
|
// TypedValue:
|
268 |
|
|
// int(typeId) Value
|
269 |
|
|
func (deb *debugger) message(indent tab) bool {
|
270 |
|
|
for {
|
271 |
|
|
// Convert the uint64 to a signed integer typeId
|
272 |
|
|
uid := deb.int64()
|
273 |
|
|
id := typeId(uid)
|
274 |
|
|
deb.dump("type id=%d", id)
|
275 |
|
|
if id < 0 {
|
276 |
|
|
deb.typeDefinition(indent, -id)
|
277 |
|
|
n := deb.loadBlock(false)
|
278 |
|
|
deb.dump("Message of length %d", n)
|
279 |
|
|
continue
|
280 |
|
|
} else {
|
281 |
|
|
deb.value(indent, id)
|
282 |
|
|
break
|
283 |
|
|
}
|
284 |
|
|
}
|
285 |
|
|
return true
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
// Helper methods to make it easy to scan a type descriptor.
|
289 |
|
|
|
290 |
|
|
// common returns the CommonType at the input point.
|
291 |
|
|
func (deb *debugger) common() CommonType {
|
292 |
|
|
fieldNum := -1
|
293 |
|
|
name := ""
|
294 |
|
|
id := typeId(0)
|
295 |
|
|
for {
|
296 |
|
|
delta := deb.delta(-1)
|
297 |
|
|
if delta == 0 {
|
298 |
|
|
break
|
299 |
|
|
}
|
300 |
|
|
fieldNum += delta
|
301 |
|
|
switch fieldNum {
|
302 |
|
|
case 0:
|
303 |
|
|
name = deb.string()
|
304 |
|
|
case 1:
|
305 |
|
|
// Id typeId
|
306 |
|
|
id = deb.typeId()
|
307 |
|
|
default:
|
308 |
|
|
errorf("corrupted CommonType")
|
309 |
|
|
}
|
310 |
|
|
}
|
311 |
|
|
return CommonType{name, id}
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
// uint returns the unsigned int at the input point, as a uint (not uint64).
|
315 |
|
|
func (deb *debugger) uint() uint {
|
316 |
|
|
return uint(deb.uint64())
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
// int returns the signed int at the input point, as an int (not int64).
|
320 |
|
|
func (deb *debugger) int() int {
|
321 |
|
|
return int(deb.int64())
|
322 |
|
|
}
|
323 |
|
|
|
324 |
|
|
// typeId returns the type id at the input point.
|
325 |
|
|
func (deb *debugger) typeId() typeId {
|
326 |
|
|
return typeId(deb.int64())
|
327 |
|
|
}
|
328 |
|
|
|
329 |
|
|
// string returns the string at the input point.
|
330 |
|
|
func (deb *debugger) string() string {
|
331 |
|
|
x := int(deb.uint64())
|
332 |
|
|
b := make([]byte, x)
|
333 |
|
|
nb, _ := deb.r.Read(b)
|
334 |
|
|
if nb != x {
|
335 |
|
|
errorf("corrupted type")
|
336 |
|
|
}
|
337 |
|
|
deb.consumed(nb)
|
338 |
|
|
return string(b)
|
339 |
|
|
}
|
340 |
|
|
|
341 |
|
|
// delta returns the field delta at the input point. The expect argument,
|
342 |
|
|
// if non-negative, identifies what the value should be.
|
343 |
|
|
func (deb *debugger) delta(expect int) int {
|
344 |
|
|
delta := int(deb.uint64())
|
345 |
|
|
if delta < 0 || (expect >= 0 && delta != expect) {
|
346 |
|
|
errorf("decode: corrupted type: delta %d expected %d", delta, expect)
|
347 |
|
|
}
|
348 |
|
|
return delta
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
// TypeDefinition:
|
352 |
|
|
// [int(-typeId) (already read)] encodingOfWireType
|
353 |
|
|
func (deb *debugger) typeDefinition(indent tab, id typeId) {
|
354 |
|
|
deb.dump("type definition for id %d", id)
|
355 |
|
|
// Encoding is of a wireType. Decode the structure as usual
|
356 |
|
|
fieldNum := -1
|
357 |
|
|
wire := new(wireType)
|
358 |
|
|
// A wireType defines a single field.
|
359 |
|
|
delta := deb.delta(-1)
|
360 |
|
|
fieldNum += delta
|
361 |
|
|
switch fieldNum {
|
362 |
|
|
case 0: // array type, one field of {{Common}, elem, length}
|
363 |
|
|
// Field number 0 is CommonType
|
364 |
|
|
deb.delta(1)
|
365 |
|
|
com := deb.common()
|
366 |
|
|
// Field number 1 is type Id of elem
|
367 |
|
|
deb.delta(1)
|
368 |
|
|
id := deb.typeId()
|
369 |
|
|
// Field number 3 is length
|
370 |
|
|
deb.delta(1)
|
371 |
|
|
length := deb.int()
|
372 |
|
|
wire.ArrayT = &arrayType{com, id, length}
|
373 |
|
|
|
374 |
|
|
case 1: // slice type, one field of {{Common}, elem}
|
375 |
|
|
// Field number 0 is CommonType
|
376 |
|
|
deb.delta(1)
|
377 |
|
|
com := deb.common()
|
378 |
|
|
// Field number 1 is type Id of elem
|
379 |
|
|
deb.delta(1)
|
380 |
|
|
id := deb.typeId()
|
381 |
|
|
wire.SliceT = &sliceType{com, id}
|
382 |
|
|
|
383 |
|
|
case 2: // struct type, one field of {{Common}, []fieldType}
|
384 |
|
|
// Field number 0 is CommonType
|
385 |
|
|
deb.delta(1)
|
386 |
|
|
com := deb.common()
|
387 |
|
|
// Field number 1 is slice of FieldType
|
388 |
|
|
deb.delta(1)
|
389 |
|
|
numField := int(deb.uint())
|
390 |
|
|
field := make([]*fieldType, numField)
|
391 |
|
|
for i := 0; i < numField; i++ {
|
392 |
|
|
field[i] = new(fieldType)
|
393 |
|
|
deb.delta(1) // field 0 of fieldType: name
|
394 |
|
|
field[i].Name = deb.string()
|
395 |
|
|
deb.delta(1) // field 1 of fieldType: id
|
396 |
|
|
field[i].Id = deb.typeId()
|
397 |
|
|
deb.delta(0) // end of fieldType
|
398 |
|
|
}
|
399 |
|
|
wire.StructT = &structType{com, field}
|
400 |
|
|
|
401 |
|
|
case 3: // map type, one field of {{Common}, key, elem}
|
402 |
|
|
// Field number 0 is CommonType
|
403 |
|
|
deb.delta(1)
|
404 |
|
|
com := deb.common()
|
405 |
|
|
// Field number 1 is type Id of key
|
406 |
|
|
deb.delta(1)
|
407 |
|
|
keyId := deb.typeId()
|
408 |
|
|
// Field number 2 is type Id of elem
|
409 |
|
|
deb.delta(1)
|
410 |
|
|
elemId := deb.typeId()
|
411 |
|
|
wire.MapT = &mapType{com, keyId, elemId}
|
412 |
|
|
case 4: // GobEncoder type, one field of {{Common}}
|
413 |
|
|
// Field number 0 is CommonType
|
414 |
|
|
deb.delta(1)
|
415 |
|
|
com := deb.common()
|
416 |
|
|
wire.GobEncoderT = &gobEncoderType{com}
|
417 |
|
|
default:
|
418 |
|
|
errorf("bad field in type %d", fieldNum)
|
419 |
|
|
}
|
420 |
|
|
deb.printWireType(indent, wire)
|
421 |
|
|
deb.delta(0) // end inner type (arrayType, etc.)
|
422 |
|
|
deb.delta(0) // end wireType
|
423 |
|
|
// Remember we've seen this type.
|
424 |
|
|
deb.wireType[id] = wire
|
425 |
|
|
}
|
426 |
|
|
|
427 |
|
|
// Value:
|
428 |
|
|
// SingletonValue | StructValue
|
429 |
|
|
func (deb *debugger) value(indent tab, id typeId) {
|
430 |
|
|
wire, ok := deb.wireType[id]
|
431 |
|
|
if ok && wire.StructT != nil {
|
432 |
|
|
deb.structValue(indent, id)
|
433 |
|
|
} else {
|
434 |
|
|
deb.singletonValue(indent, id)
|
435 |
|
|
}
|
436 |
|
|
}
|
437 |
|
|
|
438 |
|
|
// SingletonValue:
|
439 |
|
|
// uint(0) FieldValue
|
440 |
|
|
func (deb *debugger) singletonValue(indent tab, id typeId) {
|
441 |
|
|
deb.dump("Singleton value")
|
442 |
|
|
// is it a builtin type?
|
443 |
|
|
wire := deb.wireType[id]
|
444 |
|
|
_, ok := builtinIdToType[id]
|
445 |
|
|
if !ok && wire == nil {
|
446 |
|
|
errorf("type id %d not defined", id)
|
447 |
|
|
}
|
448 |
|
|
m := deb.uint64()
|
449 |
|
|
if m != 0 {
|
450 |
|
|
errorf("expected zero; got %d", m)
|
451 |
|
|
}
|
452 |
|
|
deb.fieldValue(indent, id)
|
453 |
|
|
}
|
454 |
|
|
|
455 |
|
|
// InterfaceValue:
|
456 |
|
|
// NilInterfaceValue | NonNilInterfaceValue
|
457 |
|
|
func (deb *debugger) interfaceValue(indent tab) {
|
458 |
|
|
deb.dump("Start of interface value")
|
459 |
|
|
if nameLen := deb.uint64(); nameLen == 0 {
|
460 |
|
|
deb.nilInterfaceValue(indent)
|
461 |
|
|
} else {
|
462 |
|
|
deb.nonNilInterfaceValue(indent, int(nameLen))
|
463 |
|
|
}
|
464 |
|
|
}
|
465 |
|
|
|
466 |
|
|
// NilInterfaceValue:
|
467 |
|
|
// uint(0) [already read]
|
468 |
|
|
func (deb *debugger) nilInterfaceValue(indent tab) int {
|
469 |
|
|
fmt.Fprintf(os.Stderr, "%snil interface\n", indent)
|
470 |
|
|
return 0
|
471 |
|
|
}
|
472 |
|
|
|
473 |
|
|
// NonNilInterfaceValue:
|
474 |
|
|
// ConcreteTypeName TypeSequence InterfaceContents
|
475 |
|
|
// ConcreteTypeName:
|
476 |
|
|
// uint(lengthOfName) [already read=n] name
|
477 |
|
|
// InterfaceContents:
|
478 |
|
|
// int(concreteTypeId) DelimitedValue
|
479 |
|
|
// DelimitedValue:
|
480 |
|
|
// uint(length) Value
|
481 |
|
|
func (deb *debugger) nonNilInterfaceValue(indent tab, nameLen int) {
|
482 |
|
|
// ConcreteTypeName
|
483 |
|
|
b := make([]byte, nameLen)
|
484 |
|
|
deb.r.Read(b) // TODO: CHECK THESE READS!!
|
485 |
|
|
deb.consumed(nameLen)
|
486 |
|
|
name := string(b)
|
487 |
|
|
|
488 |
|
|
for {
|
489 |
|
|
id := deb.typeId()
|
490 |
|
|
if id < 0 {
|
491 |
|
|
deb.typeDefinition(indent, -id)
|
492 |
|
|
n := deb.loadBlock(false)
|
493 |
|
|
deb.dump("Nested message of length %d", n)
|
494 |
|
|
} else {
|
495 |
|
|
// DelimitedValue
|
496 |
|
|
x := deb.uint64() // in case we want to ignore the value; we don't.
|
497 |
|
|
fmt.Fprintf(os.Stderr, "%sinterface value, type %q id=%d; valueLength %d\n", indent, name, id, x)
|
498 |
|
|
deb.value(indent, id)
|
499 |
|
|
break
|
500 |
|
|
}
|
501 |
|
|
}
|
502 |
|
|
}
|
503 |
|
|
|
504 |
|
|
// printCommonType prints a common type; used by printWireType.
|
505 |
|
|
func (deb *debugger) printCommonType(indent tab, kind string, common *CommonType) {
|
506 |
|
|
indent.print()
|
507 |
|
|
fmt.Fprintf(os.Stderr, "%s %q id=%d\n", kind, common.Name, common.Id)
|
508 |
|
|
}
|
509 |
|
|
|
510 |
|
|
// printWireType prints the contents of a wireType.
|
511 |
|
|
func (deb *debugger) printWireType(indent tab, wire *wireType) {
|
512 |
|
|
fmt.Fprintf(os.Stderr, "%stype definition {\n", indent)
|
513 |
|
|
indent++
|
514 |
|
|
switch {
|
515 |
|
|
case wire.ArrayT != nil:
|
516 |
|
|
deb.printCommonType(indent, "array", &wire.ArrayT.CommonType)
|
517 |
|
|
fmt.Fprintf(os.Stderr, "%slen %d\n", indent+1, wire.ArrayT.Len)
|
518 |
|
|
fmt.Fprintf(os.Stderr, "%selemid %d\n", indent+1, wire.ArrayT.Elem)
|
519 |
|
|
case wire.MapT != nil:
|
520 |
|
|
deb.printCommonType(indent, "map", &wire.MapT.CommonType)
|
521 |
|
|
fmt.Fprintf(os.Stderr, "%skey id=%d\n", indent+1, wire.MapT.Key)
|
522 |
|
|
fmt.Fprintf(os.Stderr, "%selem id=%d\n", indent+1, wire.MapT.Elem)
|
523 |
|
|
case wire.SliceT != nil:
|
524 |
|
|
deb.printCommonType(indent, "slice", &wire.SliceT.CommonType)
|
525 |
|
|
fmt.Fprintf(os.Stderr, "%selem id=%d\n", indent+1, wire.SliceT.Elem)
|
526 |
|
|
case wire.StructT != nil:
|
527 |
|
|
deb.printCommonType(indent, "struct", &wire.StructT.CommonType)
|
528 |
|
|
for i, field := range wire.StructT.Field {
|
529 |
|
|
fmt.Fprintf(os.Stderr, "%sfield %d:\t%s\tid=%d\n", indent+1, i, field.Name, field.Id)
|
530 |
|
|
}
|
531 |
|
|
case wire.GobEncoderT != nil:
|
532 |
|
|
deb.printCommonType(indent, "GobEncoder", &wire.GobEncoderT.CommonType)
|
533 |
|
|
}
|
534 |
|
|
indent--
|
535 |
|
|
fmt.Fprintf(os.Stderr, "%s}\n", indent)
|
536 |
|
|
}
|
537 |
|
|
|
538 |
|
|
// fieldValue prints a value of any type, such as a struct field.
|
539 |
|
|
// FieldValue:
|
540 |
|
|
// builtinValue | ArrayValue | MapValue | SliceValue | StructValue | InterfaceValue
|
541 |
|
|
func (deb *debugger) fieldValue(indent tab, id typeId) {
|
542 |
|
|
_, ok := builtinIdToType[id]
|
543 |
|
|
if ok {
|
544 |
|
|
if id == tInterface {
|
545 |
|
|
deb.interfaceValue(indent)
|
546 |
|
|
} else {
|
547 |
|
|
deb.printBuiltin(indent, id)
|
548 |
|
|
}
|
549 |
|
|
return
|
550 |
|
|
}
|
551 |
|
|
wire, ok := deb.wireType[id]
|
552 |
|
|
if !ok {
|
553 |
|
|
errorf("type id %d not defined", id)
|
554 |
|
|
}
|
555 |
|
|
switch {
|
556 |
|
|
case wire.ArrayT != nil:
|
557 |
|
|
deb.arrayValue(indent, wire)
|
558 |
|
|
case wire.MapT != nil:
|
559 |
|
|
deb.mapValue(indent, wire)
|
560 |
|
|
case wire.SliceT != nil:
|
561 |
|
|
deb.sliceValue(indent, wire)
|
562 |
|
|
case wire.StructT != nil:
|
563 |
|
|
deb.structValue(indent, id)
|
564 |
|
|
case wire.GobEncoderT != nil:
|
565 |
|
|
deb.gobEncoderValue(indent, id)
|
566 |
|
|
default:
|
567 |
|
|
panic("bad wire type for field")
|
568 |
|
|
}
|
569 |
|
|
}
|
570 |
|
|
|
571 |
|
|
// printBuiltin prints a value not of a fundamental type, that is,
|
572 |
|
|
// one whose type is known to gobs at bootstrap time.
|
573 |
|
|
func (deb *debugger) printBuiltin(indent tab, id typeId) {
|
574 |
|
|
switch id {
|
575 |
|
|
case tBool:
|
576 |
|
|
x := deb.int64()
|
577 |
|
|
if x == 0 {
|
578 |
|
|
fmt.Fprintf(os.Stderr, "%sfalse\n", indent)
|
579 |
|
|
} else {
|
580 |
|
|
fmt.Fprintf(os.Stderr, "%strue\n", indent)
|
581 |
|
|
}
|
582 |
|
|
case tInt:
|
583 |
|
|
x := deb.int64()
|
584 |
|
|
fmt.Fprintf(os.Stderr, "%s%d\n", indent, x)
|
585 |
|
|
case tUint:
|
586 |
|
|
x := deb.int64()
|
587 |
|
|
fmt.Fprintf(os.Stderr, "%s%d\n", indent, x)
|
588 |
|
|
case tFloat:
|
589 |
|
|
x := deb.uint64()
|
590 |
|
|
fmt.Fprintf(os.Stderr, "%s%g\n", indent, floatFromBits(x))
|
591 |
|
|
case tComplex:
|
592 |
|
|
r := deb.uint64()
|
593 |
|
|
i := deb.uint64()
|
594 |
|
|
fmt.Fprintf(os.Stderr, "%s%g+%gi\n", indent, floatFromBits(r), floatFromBits(i))
|
595 |
|
|
case tBytes:
|
596 |
|
|
x := int(deb.uint64())
|
597 |
|
|
b := make([]byte, x)
|
598 |
|
|
deb.r.Read(b)
|
599 |
|
|
deb.consumed(x)
|
600 |
|
|
fmt.Fprintf(os.Stderr, "%s{% x}=%q\n", indent, b, b)
|
601 |
|
|
case tString:
|
602 |
|
|
x := int(deb.uint64())
|
603 |
|
|
b := make([]byte, x)
|
604 |
|
|
deb.r.Read(b)
|
605 |
|
|
deb.consumed(x)
|
606 |
|
|
fmt.Fprintf(os.Stderr, "%s%q\n", indent, b)
|
607 |
|
|
default:
|
608 |
|
|
panic("unknown builtin")
|
609 |
|
|
}
|
610 |
|
|
}
|
611 |
|
|
|
612 |
|
|
// ArrayValue:
|
613 |
|
|
// uint(n) FieldValue*n
|
614 |
|
|
func (deb *debugger) arrayValue(indent tab, wire *wireType) {
|
615 |
|
|
elemId := wire.ArrayT.Elem
|
616 |
|
|
u := deb.uint64()
|
617 |
|
|
length := int(u)
|
618 |
|
|
for i := 0; i < length; i++ {
|
619 |
|
|
deb.fieldValue(indent, elemId)
|
620 |
|
|
}
|
621 |
|
|
if length != wire.ArrayT.Len {
|
622 |
|
|
fmt.Fprintf(os.Stderr, "%s(wrong length for array: %d should be %d)\n", indent, length, wire.ArrayT.Len)
|
623 |
|
|
}
|
624 |
|
|
}
|
625 |
|
|
|
626 |
|
|
// MapValue:
|
627 |
|
|
// uint(n) (FieldValue FieldValue)*n [n (key, value) pairs]
|
628 |
|
|
func (deb *debugger) mapValue(indent tab, wire *wireType) {
|
629 |
|
|
keyId := wire.MapT.Key
|
630 |
|
|
elemId := wire.MapT.Elem
|
631 |
|
|
u := deb.uint64()
|
632 |
|
|
length := int(u)
|
633 |
|
|
for i := 0; i < length; i++ {
|
634 |
|
|
deb.fieldValue(indent+1, keyId)
|
635 |
|
|
deb.fieldValue(indent+1, elemId)
|
636 |
|
|
}
|
637 |
|
|
}
|
638 |
|
|
|
639 |
|
|
// SliceValue:
|
640 |
|
|
// uint(n) (n FieldValue)
|
641 |
|
|
func (deb *debugger) sliceValue(indent tab, wire *wireType) {
|
642 |
|
|
elemId := wire.SliceT.Elem
|
643 |
|
|
u := deb.uint64()
|
644 |
|
|
length := int(u)
|
645 |
|
|
deb.dump("Start of slice of length %d", length)
|
646 |
|
|
|
647 |
|
|
for i := 0; i < length; i++ {
|
648 |
|
|
deb.fieldValue(indent, elemId)
|
649 |
|
|
}
|
650 |
|
|
}
|
651 |
|
|
|
652 |
|
|
// StructValue:
|
653 |
|
|
// (uint(fieldDelta) FieldValue)*
|
654 |
|
|
func (deb *debugger) structValue(indent tab, id typeId) {
|
655 |
|
|
deb.dump("Start of struct value of %q id=%d\n<<\n", id.name(), id)
|
656 |
|
|
fmt.Fprintf(os.Stderr, "%s%s struct {\n", indent, id.name())
|
657 |
|
|
wire, ok := deb.wireType[id]
|
658 |
|
|
if !ok {
|
659 |
|
|
errorf("type id %d not defined", id)
|
660 |
|
|
}
|
661 |
|
|
strct := wire.StructT
|
662 |
|
|
fieldNum := -1
|
663 |
|
|
indent++
|
664 |
|
|
for {
|
665 |
|
|
delta := deb.uint64()
|
666 |
|
|
if delta == 0 { // struct terminator is zero delta fieldnum
|
667 |
|
|
break
|
668 |
|
|
}
|
669 |
|
|
fieldNum += int(delta)
|
670 |
|
|
if fieldNum < 0 || fieldNum >= len(strct.Field) {
|
671 |
|
|
deb.dump("field number out of range: prevField=%d delta=%d", fieldNum-int(delta), delta)
|
672 |
|
|
break
|
673 |
|
|
}
|
674 |
|
|
fmt.Fprintf(os.Stderr, "%sfield %d:\t%s\n", indent, fieldNum, wire.StructT.Field[fieldNum].Name)
|
675 |
|
|
deb.fieldValue(indent+1, strct.Field[fieldNum].Id)
|
676 |
|
|
}
|
677 |
|
|
indent--
|
678 |
|
|
fmt.Fprintf(os.Stderr, "%s} // end %s struct\n", indent, id.name())
|
679 |
|
|
deb.dump(">> End of struct value of type %d %q", id, id.name())
|
680 |
|
|
}
|
681 |
|
|
|
682 |
|
|
// GobEncoderValue:
|
683 |
|
|
// uint(n) byte*n
|
684 |
|
|
func (deb *debugger) gobEncoderValue(indent tab, id typeId) {
|
685 |
|
|
len := deb.uint64()
|
686 |
|
|
deb.dump("GobEncoder value of %q id=%d, length %d\n", id.name(), id, len)
|
687 |
|
|
fmt.Fprintf(os.Stderr, "%s%s (implements GobEncoder)\n", indent, id.name())
|
688 |
|
|
data := make([]byte, len)
|
689 |
|
|
_, err := deb.r.Read(data)
|
690 |
|
|
if err != nil {
|
691 |
|
|
errorf("gobEncoder data read: %s", err)
|
692 |
|
|
}
|
693 |
|
|
fmt.Fprintf(os.Stderr, "%s[% .2x]\n", indent+1, data)
|
694 |
|
|
}
|