debug.go 18 KB

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