dump.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (c) 2013 Dave Collins <dave@davec.name>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. package spew
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "fmt"
  21. "io"
  22. "os"
  23. "reflect"
  24. "regexp"
  25. "strconv"
  26. "strings"
  27. )
  28. var (
  29. // uint8Type is a reflect.Type representing a uint8. It is used to
  30. // convert cgo types to uint8 slices for hexdumping.
  31. uint8Type = reflect.TypeOf(uint8(0))
  32. // cCharRE is a regular expression that matches a cgo char.
  33. // It is used to detect character arrays to hexdump them.
  34. cCharRE = regexp.MustCompile("^.*\\._Ctype_char$")
  35. // cUnsignedCharRE is a regular expression that matches a cgo unsigned
  36. // char. It is used to detect unsigned character arrays to hexdump
  37. // them.
  38. cUnsignedCharRE = regexp.MustCompile("^.*\\._Ctype_unsignedchar$")
  39. // cUint8tCharRE is a regular expression that matches a cgo uint8_t.
  40. // It is used to detect uint8_t arrays to hexdump them.
  41. cUint8tCharRE = regexp.MustCompile("^.*\\._Ctype_uint8_t$")
  42. )
  43. // dumpState contains information about the state of a dump operation.
  44. type dumpState struct {
  45. w io.Writer
  46. depth int
  47. pointers map[uintptr]int
  48. ignoreNextType bool
  49. ignoreNextIndent bool
  50. cs *ConfigState
  51. }
  52. // indent performs indentation according to the depth level and cs.Indent
  53. // option.
  54. func (d *dumpState) indent() {
  55. if d.ignoreNextIndent {
  56. d.ignoreNextIndent = false
  57. return
  58. }
  59. d.w.Write(bytes.Repeat([]byte(d.cs.Indent), d.depth))
  60. }
  61. // unpackValue returns values inside of non-nil interfaces when possible.
  62. // This is useful for data types like structs, arrays, slices, and maps which
  63. // can contain varying types packed inside an interface.
  64. func (d *dumpState) unpackValue(v reflect.Value) reflect.Value {
  65. if v.Kind() == reflect.Interface && !v.IsNil() {
  66. v = v.Elem()
  67. }
  68. return v
  69. }
  70. // dumpPtr handles formatting of pointers by indirecting them as necessary.
  71. func (d *dumpState) dumpPtr(v reflect.Value) {
  72. // Remove pointers at or below the current depth from map used to detect
  73. // circular refs.
  74. for k, depth := range d.pointers {
  75. if depth >= d.depth {
  76. delete(d.pointers, k)
  77. }
  78. }
  79. // Keep list of all dereferenced pointers to show later.
  80. pointerChain := make([]uintptr, 0)
  81. // Figure out how many levels of indirection there are by dereferencing
  82. // pointers and unpacking interfaces down the chain while detecting circular
  83. // references.
  84. nilFound := false
  85. cycleFound := false
  86. indirects := 0
  87. ve := v
  88. for ve.Kind() == reflect.Ptr {
  89. if ve.IsNil() {
  90. nilFound = true
  91. break
  92. }
  93. indirects++
  94. addr := ve.Pointer()
  95. pointerChain = append(pointerChain, addr)
  96. if pd, ok := d.pointers[addr]; ok && pd < d.depth {
  97. cycleFound = true
  98. indirects--
  99. break
  100. }
  101. d.pointers[addr] = d.depth
  102. ve = ve.Elem()
  103. if ve.Kind() == reflect.Interface {
  104. if ve.IsNil() {
  105. nilFound = true
  106. break
  107. }
  108. ve = ve.Elem()
  109. }
  110. }
  111. // Display type information.
  112. d.w.Write(openParenBytes)
  113. d.w.Write(bytes.Repeat(asteriskBytes, indirects))
  114. d.w.Write([]byte(ve.Type().String()))
  115. d.w.Write(closeParenBytes)
  116. // Display pointer information.
  117. if len(pointerChain) > 0 {
  118. d.w.Write(openParenBytes)
  119. for i, addr := range pointerChain {
  120. if i > 0 {
  121. d.w.Write(pointerChainBytes)
  122. }
  123. printHexPtr(d.w, addr)
  124. }
  125. d.w.Write(closeParenBytes)
  126. }
  127. // Display dereferenced value.
  128. d.w.Write(openParenBytes)
  129. switch {
  130. case nilFound == true:
  131. d.w.Write(nilAngleBytes)
  132. case cycleFound == true:
  133. d.w.Write(circularBytes)
  134. default:
  135. d.ignoreNextType = true
  136. d.dump(ve)
  137. }
  138. d.w.Write(closeParenBytes)
  139. }
  140. // dumpSlice handles formatting of arrays and slices. Byte (uint8 under
  141. // reflection) arrays and slices are dumped in hexdump -C fashion.
  142. func (d *dumpState) dumpSlice(v reflect.Value) {
  143. // Determine whether this type should be hex dumped or not. Also,
  144. // for types which should be hexdumped, try to use the underlying data
  145. // first, then fall back to trying to convert them to a uint8 slice.
  146. var buf []uint8
  147. doConvert := false
  148. doHexDump := false
  149. numEntries := v.Len()
  150. if numEntries > 0 {
  151. vt := v.Index(0).Type()
  152. vts := vt.String()
  153. switch {
  154. // C types that need to be converted.
  155. case cCharRE.MatchString(vts):
  156. fallthrough
  157. case cUnsignedCharRE.MatchString(vts):
  158. fallthrough
  159. case cUint8tCharRE.MatchString(vts):
  160. doConvert = true
  161. // Try to use existing uint8 slices and fall back to converting
  162. // and copying if that fails.
  163. case vt.Kind() == reflect.Uint8:
  164. // TODO(davec): Fix up the disableUnsafe bits...
  165. // We need an addressable interface to convert the type
  166. // to a byte slice. However, the reflect package won't
  167. // give us an interface on certain things like
  168. // unexported struct fields in order to enforce
  169. // visibility rules. We use unsafe, when available, to
  170. // bypass these restrictions since this package does not
  171. // mutate the values.
  172. vs := v
  173. if !vs.CanInterface() || !vs.CanAddr() {
  174. vs = unsafeReflectValue(vs)
  175. }
  176. if !UnsafeDisabled {
  177. vs = vs.Slice(0, numEntries)
  178. // Use the existing uint8 slice if it can be
  179. // type asserted.
  180. iface := vs.Interface()
  181. if slice, ok := iface.([]uint8); ok {
  182. buf = slice
  183. doHexDump = true
  184. break
  185. }
  186. }
  187. // The underlying data needs to be converted if it can't
  188. // be type asserted to a uint8 slice.
  189. doConvert = true
  190. }
  191. // Copy and convert the underlying type if needed.
  192. if doConvert && vt.ConvertibleTo(uint8Type) {
  193. // Convert and copy each element into a uint8 byte
  194. // slice.
  195. buf = make([]uint8, numEntries)
  196. for i := 0; i < numEntries; i++ {
  197. vv := v.Index(i)
  198. buf[i] = uint8(vv.Convert(uint8Type).Uint())
  199. }
  200. doHexDump = true
  201. }
  202. }
  203. // Hexdump the entire slice as needed.
  204. if doHexDump {
  205. indent := strings.Repeat(d.cs.Indent, d.depth)
  206. str := indent + hex.Dump(buf)
  207. str = strings.Replace(str, "\n", "\n"+indent, -1)
  208. str = strings.TrimRight(str, d.cs.Indent)
  209. d.w.Write([]byte(str))
  210. return
  211. }
  212. // Recursively call dump for each item.
  213. for i := 0; i < numEntries; i++ {
  214. d.dump(d.unpackValue(v.Index(i)))
  215. if i < (numEntries - 1) {
  216. d.w.Write(commaNewlineBytes)
  217. } else {
  218. d.w.Write(newlineBytes)
  219. }
  220. }
  221. }
  222. // dump is the main workhorse for dumping a value. It uses the passed reflect
  223. // value to figure out what kind of object we are dealing with and formats it
  224. // appropriately. It is a recursive function, however circular data structures
  225. // are detected and handled properly.
  226. func (d *dumpState) dump(v reflect.Value) {
  227. // Handle invalid reflect values immediately.
  228. kind := v.Kind()
  229. if kind == reflect.Invalid {
  230. d.w.Write(invalidAngleBytes)
  231. return
  232. }
  233. // Handle pointers specially.
  234. if kind == reflect.Ptr {
  235. d.indent()
  236. d.dumpPtr(v)
  237. return
  238. }
  239. // Print type information unless already handled elsewhere.
  240. if !d.ignoreNextType {
  241. d.indent()
  242. d.w.Write(openParenBytes)
  243. d.w.Write([]byte(v.Type().String()))
  244. d.w.Write(closeParenBytes)
  245. d.w.Write(spaceBytes)
  246. }
  247. d.ignoreNextType = false
  248. // Display length and capacity if the built-in len and cap functions
  249. // work with the value's kind and the len/cap itself is non-zero.
  250. valueLen, valueCap := 0, 0
  251. switch v.Kind() {
  252. case reflect.Array, reflect.Slice, reflect.Chan:
  253. valueLen, valueCap = v.Len(), v.Cap()
  254. case reflect.Map, reflect.String:
  255. valueLen = v.Len()
  256. }
  257. if valueLen != 0 || valueCap != 0 {
  258. d.w.Write(openParenBytes)
  259. if valueLen != 0 {
  260. d.w.Write(lenEqualsBytes)
  261. printInt(d.w, int64(valueLen), 10)
  262. }
  263. if valueCap != 0 {
  264. if valueLen != 0 {
  265. d.w.Write(spaceBytes)
  266. }
  267. d.w.Write(capEqualsBytes)
  268. printInt(d.w, int64(valueCap), 10)
  269. }
  270. d.w.Write(closeParenBytes)
  271. d.w.Write(spaceBytes)
  272. }
  273. // Call Stringer/error interfaces if they exist and the handle methods flag
  274. // is enabled
  275. if !d.cs.DisableMethods {
  276. if (kind != reflect.Invalid) && (kind != reflect.Interface) {
  277. if handled := handleMethods(d.cs, d.w, v); handled {
  278. return
  279. }
  280. }
  281. }
  282. switch kind {
  283. case reflect.Invalid:
  284. // Do nothing. We should never get here since invalid has already
  285. // been handled above.
  286. case reflect.Bool:
  287. printBool(d.w, v.Bool())
  288. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
  289. printInt(d.w, v.Int(), 10)
  290. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  291. printUint(d.w, v.Uint(), 10)
  292. case reflect.Float32:
  293. printFloat(d.w, v.Float(), 32)
  294. case reflect.Float64:
  295. printFloat(d.w, v.Float(), 64)
  296. case reflect.Complex64:
  297. printComplex(d.w, v.Complex(), 32)
  298. case reflect.Complex128:
  299. printComplex(d.w, v.Complex(), 64)
  300. case reflect.Slice:
  301. if v.IsNil() {
  302. d.w.Write(nilAngleBytes)
  303. break
  304. }
  305. fallthrough
  306. case reflect.Array:
  307. d.w.Write(openBraceNewlineBytes)
  308. d.depth++
  309. if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {
  310. d.indent()
  311. d.w.Write(maxNewlineBytes)
  312. } else {
  313. d.dumpSlice(v)
  314. }
  315. d.depth--
  316. d.indent()
  317. d.w.Write(closeBraceBytes)
  318. case reflect.String:
  319. d.w.Write([]byte(strconv.Quote(v.String())))
  320. case reflect.Interface:
  321. // The only time we should get here is for nil interfaces due to
  322. // unpackValue calls.
  323. if v.IsNil() {
  324. d.w.Write(nilAngleBytes)
  325. }
  326. case reflect.Ptr:
  327. // Do nothing. We should never get here since pointers have already
  328. // been handled above.
  329. case reflect.Map:
  330. // nil maps should be indicated as different than empty maps
  331. if v.IsNil() {
  332. d.w.Write(nilAngleBytes)
  333. break
  334. }
  335. d.w.Write(openBraceNewlineBytes)
  336. d.depth++
  337. if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {
  338. d.indent()
  339. d.w.Write(maxNewlineBytes)
  340. } else {
  341. numEntries := v.Len()
  342. keys := v.MapKeys()
  343. if d.cs.SortKeys {
  344. sortValues(keys, d.cs)
  345. }
  346. for i, key := range keys {
  347. d.dump(d.unpackValue(key))
  348. d.w.Write(colonSpaceBytes)
  349. d.ignoreNextIndent = true
  350. d.dump(d.unpackValue(v.MapIndex(key)))
  351. if i < (numEntries - 1) {
  352. d.w.Write(commaNewlineBytes)
  353. } else {
  354. d.w.Write(newlineBytes)
  355. }
  356. }
  357. }
  358. d.depth--
  359. d.indent()
  360. d.w.Write(closeBraceBytes)
  361. case reflect.Struct:
  362. d.w.Write(openBraceNewlineBytes)
  363. d.depth++
  364. if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {
  365. d.indent()
  366. d.w.Write(maxNewlineBytes)
  367. } else {
  368. vt := v.Type()
  369. numFields := v.NumField()
  370. for i := 0; i < numFields; i++ {
  371. d.indent()
  372. vtf := vt.Field(i)
  373. d.w.Write([]byte(vtf.Name))
  374. d.w.Write(colonSpaceBytes)
  375. d.ignoreNextIndent = true
  376. d.dump(d.unpackValue(v.Field(i)))
  377. if i < (numFields - 1) {
  378. d.w.Write(commaNewlineBytes)
  379. } else {
  380. d.w.Write(newlineBytes)
  381. }
  382. }
  383. }
  384. d.depth--
  385. d.indent()
  386. d.w.Write(closeBraceBytes)
  387. case reflect.Uintptr:
  388. printHexPtr(d.w, uintptr(v.Uint()))
  389. case reflect.UnsafePointer, reflect.Chan, reflect.Func:
  390. printHexPtr(d.w, v.Pointer())
  391. // There were not any other types at the time this code was written, but
  392. // fall back to letting the default fmt package handle it in case any new
  393. // types are added.
  394. default:
  395. if v.CanInterface() {
  396. fmt.Fprintf(d.w, "%v", v.Interface())
  397. } else {
  398. fmt.Fprintf(d.w, "%v", v.String())
  399. }
  400. }
  401. }
  402. // fdump is a helper function to consolidate the logic from the various public
  403. // methods which take varying writers and config states.
  404. func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
  405. for _, arg := range a {
  406. if arg == nil {
  407. w.Write(interfaceBytes)
  408. w.Write(spaceBytes)
  409. w.Write(nilAngleBytes)
  410. w.Write(newlineBytes)
  411. continue
  412. }
  413. d := dumpState{w: w, cs: cs}
  414. d.pointers = make(map[uintptr]int)
  415. d.dump(reflect.ValueOf(arg))
  416. d.w.Write(newlineBytes)
  417. }
  418. }
  419. // Fdump formats and displays the passed arguments to io.Writer w. It formats
  420. // exactly the same as Dump.
  421. func Fdump(w io.Writer, a ...interface{}) {
  422. fdump(&Config, w, a...)
  423. }
  424. // Sdump returns a string with the passed arguments formatted exactly the same
  425. // as Dump.
  426. func Sdump(a ...interface{}) string {
  427. var buf bytes.Buffer
  428. fdump(&Config, &buf, a...)
  429. return buf.String()
  430. }
  431. /*
  432. Dump displays the passed parameters to standard out with newlines, customizable
  433. indentation, and additional debug information such as complete types and all
  434. pointer addresses used to indirect to the final value. It provides the
  435. following features over the built-in printing facilities provided by the fmt
  436. package:
  437. * Pointers are dereferenced and followed
  438. * Circular data structures are detected and handled properly
  439. * Custom Stringer/error interfaces are optionally invoked, including
  440. on unexported types
  441. * Custom types which only implement the Stringer/error interfaces via
  442. a pointer receiver are optionally invoked when passing non-pointer
  443. variables
  444. * Byte arrays and slices are dumped like the hexdump -C command which
  445. includes offsets, byte values in hex, and ASCII output
  446. The configuration options are controlled by an exported package global,
  447. spew.Config. See ConfigState for options documentation.
  448. See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to
  449. get the formatted result as a string.
  450. */
  451. func Dump(a ...interface{}) {
  452. fdump(&Config, os.Stdout, a...)
  453. }