dump.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. // We need an addressable interface to convert the type back
  165. // into a byte slice. However, the reflect package won't give
  166. // us an interface on certain things like unexported struct
  167. // fields in order to enforce visibility rules. We use unsafe
  168. // to bypass these restrictions since this package does not
  169. // mutate the values.
  170. vs := v
  171. if !vs.CanInterface() || !vs.CanAddr() {
  172. vs = unsafeReflectValue(vs)
  173. }
  174. vs = vs.Slice(0, numEntries)
  175. // Use the existing uint8 slice if it can be type
  176. // asserted.
  177. iface := vs.Interface()
  178. if slice, ok := iface.([]uint8); ok {
  179. buf = slice
  180. doHexDump = true
  181. break
  182. }
  183. // The underlying data needs to be converted if it can't
  184. // be type asserted to a uint8 slice.
  185. doConvert = true
  186. }
  187. // Copy and convert the underlying type if needed.
  188. if doConvert && vt.ConvertibleTo(uint8Type) {
  189. // Convert and copy each element into a uint8 byte
  190. // slice.
  191. buf = make([]uint8, numEntries)
  192. for i := 0; i < numEntries; i++ {
  193. vv := v.Index(i)
  194. buf[i] = uint8(vv.Convert(uint8Type).Uint())
  195. }
  196. doHexDump = true
  197. }
  198. }
  199. // Hexdump the entire slice as needed.
  200. if doHexDump {
  201. indent := strings.Repeat(d.cs.Indent, d.depth)
  202. str := indent + hex.Dump(buf)
  203. str = strings.Replace(str, "\n", "\n"+indent, -1)
  204. str = strings.TrimRight(str, d.cs.Indent)
  205. d.w.Write([]byte(str))
  206. return
  207. }
  208. // Recursively call dump for each item.
  209. for i := 0; i < numEntries; i++ {
  210. d.dump(d.unpackValue(v.Index(i)))
  211. if i < (numEntries - 1) {
  212. d.w.Write(commaNewlineBytes)
  213. } else {
  214. d.w.Write(newlineBytes)
  215. }
  216. }
  217. }
  218. // dump is the main workhorse for dumping a value. It uses the passed reflect
  219. // value to figure out what kind of object we are dealing with and formats it
  220. // appropriately. It is a recursive function, however circular data structures
  221. // are detected and handled properly.
  222. func (d *dumpState) dump(v reflect.Value) {
  223. // Handle invalid reflect values immediately.
  224. kind := v.Kind()
  225. if kind == reflect.Invalid {
  226. d.w.Write(invalidAngleBytes)
  227. return
  228. }
  229. // Handle pointers specially.
  230. if kind == reflect.Ptr {
  231. d.indent()
  232. d.dumpPtr(v)
  233. return
  234. }
  235. // Print type information unless already handled elsewhere.
  236. if !d.ignoreNextType {
  237. d.indent()
  238. d.w.Write(openParenBytes)
  239. d.w.Write([]byte(v.Type().String()))
  240. d.w.Write(closeParenBytes)
  241. d.w.Write(spaceBytes)
  242. }
  243. d.ignoreNextType = false
  244. // Call Stringer/error interfaces if they exist and the handle methods flag
  245. // is enabled
  246. if !d.cs.DisableMethods {
  247. if (kind != reflect.Invalid) && (kind != reflect.Interface) {
  248. if handled := handleMethods(d.cs, d.w, v); handled {
  249. return
  250. }
  251. }
  252. }
  253. switch kind {
  254. case reflect.Invalid:
  255. // Do nothing. We should never get here since invalid has already
  256. // been handled above.
  257. case reflect.Bool:
  258. printBool(d.w, v.Bool())
  259. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
  260. printInt(d.w, v.Int(), 10)
  261. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  262. printUint(d.w, v.Uint(), 10)
  263. case reflect.Float32:
  264. printFloat(d.w, v.Float(), 32)
  265. case reflect.Float64:
  266. printFloat(d.w, v.Float(), 64)
  267. case reflect.Complex64:
  268. printComplex(d.w, v.Complex(), 32)
  269. case reflect.Complex128:
  270. printComplex(d.w, v.Complex(), 64)
  271. case reflect.Slice:
  272. if v.IsNil() {
  273. d.w.Write(nilAngleBytes)
  274. break
  275. }
  276. fallthrough
  277. case reflect.Array:
  278. d.w.Write(openBraceNewlineBytes)
  279. d.depth++
  280. if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {
  281. d.indent()
  282. d.w.Write(maxNewlineBytes)
  283. } else {
  284. d.dumpSlice(v)
  285. }
  286. d.depth--
  287. d.indent()
  288. d.w.Write(closeBraceBytes)
  289. case reflect.String:
  290. d.w.Write([]byte(strconv.Quote(v.String())))
  291. case reflect.Interface:
  292. // The only time we should get here is for nil interfaces due to
  293. // unpackValue calls.
  294. if v.IsNil() {
  295. d.w.Write(nilAngleBytes)
  296. }
  297. case reflect.Ptr:
  298. // Do nothing. We should never get here since pointers have already
  299. // been handled above.
  300. case reflect.Map:
  301. d.w.Write(openBraceNewlineBytes)
  302. d.depth++
  303. if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {
  304. d.indent()
  305. d.w.Write(maxNewlineBytes)
  306. } else {
  307. numEntries := v.Len()
  308. keys := v.MapKeys()
  309. if d.cs.SortKeys {
  310. sortValues(keys)
  311. }
  312. for i, key := range keys {
  313. d.dump(d.unpackValue(key))
  314. d.w.Write(colonSpaceBytes)
  315. d.ignoreNextIndent = true
  316. d.dump(d.unpackValue(v.MapIndex(key)))
  317. if i < (numEntries - 1) {
  318. d.w.Write(commaNewlineBytes)
  319. } else {
  320. d.w.Write(newlineBytes)
  321. }
  322. }
  323. }
  324. d.depth--
  325. d.indent()
  326. d.w.Write(closeBraceBytes)
  327. case reflect.Struct:
  328. d.w.Write(openBraceNewlineBytes)
  329. d.depth++
  330. if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {
  331. d.indent()
  332. d.w.Write(maxNewlineBytes)
  333. } else {
  334. vt := v.Type()
  335. numFields := v.NumField()
  336. for i := 0; i < numFields; i++ {
  337. d.indent()
  338. vtf := vt.Field(i)
  339. d.w.Write([]byte(vtf.Name))
  340. d.w.Write(colonSpaceBytes)
  341. d.ignoreNextIndent = true
  342. d.dump(d.unpackValue(v.Field(i)))
  343. if i < (numFields - 1) {
  344. d.w.Write(commaNewlineBytes)
  345. } else {
  346. d.w.Write(newlineBytes)
  347. }
  348. }
  349. }
  350. d.depth--
  351. d.indent()
  352. d.w.Write(closeBraceBytes)
  353. case reflect.Uintptr:
  354. printHexPtr(d.w, uintptr(v.Uint()))
  355. case reflect.UnsafePointer, reflect.Chan, reflect.Func:
  356. printHexPtr(d.w, v.Pointer())
  357. // There were not any other types at the time this code was written, but
  358. // fall back to letting the default fmt package handle it in case any new
  359. // types are added.
  360. default:
  361. if v.CanInterface() {
  362. fmt.Fprintf(d.w, "%v", v.Interface())
  363. } else {
  364. fmt.Fprintf(d.w, "%v", v.String())
  365. }
  366. }
  367. }
  368. // fdump is a helper function to consolidate the logic from the various public
  369. // methods which take varying writers and config states.
  370. func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
  371. for _, arg := range a {
  372. if arg == nil {
  373. w.Write(interfaceBytes)
  374. w.Write(spaceBytes)
  375. w.Write(nilAngleBytes)
  376. w.Write(newlineBytes)
  377. continue
  378. }
  379. d := dumpState{w: w, cs: cs}
  380. d.pointers = make(map[uintptr]int)
  381. d.dump(reflect.ValueOf(arg))
  382. d.w.Write(newlineBytes)
  383. }
  384. }
  385. // Fdump formats and displays the passed arguments to io.Writer w. It formats
  386. // exactly the same as Dump.
  387. func Fdump(w io.Writer, a ...interface{}) {
  388. fdump(&Config, w, a...)
  389. }
  390. // Sdump returns a string with the passed arguments formatted exactly the same
  391. // as Dump.
  392. func Sdump(a ...interface{}) string {
  393. var buf bytes.Buffer
  394. fdump(&Config, &buf, a...)
  395. return buf.String()
  396. }
  397. /*
  398. Dump displays the passed parameters to standard out with newlines, customizable
  399. indentation, and additional debug information such as complete types and all
  400. pointer addresses used to indirect to the final value. It provides the
  401. following features over the built-in printing facilities provided by the fmt
  402. package:
  403. * Pointers are dereferenced and followed
  404. * Circular data structures are detected and handled properly
  405. * Custom Stringer/error interfaces are optionally invoked, including
  406. on unexported types
  407. * Custom types which only implement the Stringer/error interfaces via
  408. a pointer receiver are optionally invoked when passing non-pointer
  409. variables
  410. * Byte arrays and slices are dumped like the hexdump -C command which
  411. includes offsets, byte values in hex, and ASCII output
  412. The configuration options are controlled by an exported package global,
  413. spew.Config. See ConfigState for options documentation.
  414. See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to
  415. get the formatted result as a string.
  416. */
  417. func Dump(a ...interface{}) {
  418. fdump(&Config, os.Stdout, a...)
  419. }