common.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. "fmt"
  20. "io"
  21. "reflect"
  22. "sort"
  23. "strconv"
  24. "unsafe"
  25. )
  26. const (
  27. // ptrSize is the size of a pointer on the current arch.
  28. ptrSize = unsafe.Sizeof((*byte)(nil))
  29. )
  30. var (
  31. // offsetPtr, offsetScalar, and offsetFlag are the offsets for the
  32. // internal reflect.Value fields. These values are valid before golang
  33. // commit ecccf07e7f9d which changed the format. The are also valid
  34. // after commit 82f48826c6c7 which changed the format again to mirror
  35. // the original format. Code in the init function updates these offsets
  36. // as necessary.
  37. offsetPtr = uintptr(ptrSize)
  38. offsetScalar = uintptr(0)
  39. offsetFlag = uintptr(ptrSize * 2)
  40. // flagKindWidth and flagKindShift indicate various bits that the
  41. // reflect package uses internally to track kind information.
  42. //
  43. // flagRO indicates whether or not the value field of a reflect.Value is
  44. // read-only.
  45. //
  46. // flagIndir indicates whether the value field of a reflect.Value is
  47. // the actual data or a pointer to the data.
  48. //
  49. // These values are valid before golang commit 90a7c3c86944 which
  50. // changed their positions. Code in the init function updates these
  51. // flags as necessary.
  52. flagKindWidth = uintptr(5)
  53. flagKindShift = uintptr(flagKindWidth - 1)
  54. flagRO = uintptr(1 << 0)
  55. flagIndir = uintptr(1 << 1)
  56. )
  57. func init() {
  58. // Older versions of reflect.Value stored small integers directly in the
  59. // ptr field (which is named val in the older versions). Versions
  60. // between commits ecccf07e7f9d and 82f48826c6c7 added a new field named
  61. // scalar for this purpose which unfortunately came before the flag
  62. // field, so the offset of the flag field is different for those
  63. // versions.
  64. //
  65. // This code constructs a new reflect.Value from a known small integer
  66. // and checks if the size of the reflect.Value struct indicates it has
  67. // the scalar field. When it does, the offsets are updated accordingly.
  68. vv := reflect.ValueOf(0xf00)
  69. if unsafe.Sizeof(vv) == (ptrSize * 4) {
  70. offsetScalar = ptrSize * 2
  71. offsetFlag = ptrSize * 3
  72. }
  73. // Commit 90a7c3c86944 changed the flag positions such that the low
  74. // order bits are the kind. This code extracts the kind from the flags
  75. // field and ensures it's the correct type. When it's not, the flag
  76. // order has been changed to the newer format, so the flags are updated
  77. // accordingly.
  78. upf := unsafe.Pointer(uintptr(unsafe.Pointer(&vv)) + offsetFlag)
  79. upfv := *(*uintptr)(upf)
  80. flagKindMask := uintptr((1<<flagKindWidth - 1) << flagKindShift)
  81. if (upfv&flagKindMask)>>flagKindShift != uintptr(reflect.Int) {
  82. flagKindShift = 0
  83. flagRO = 1 << 5
  84. flagIndir = 1 << 6
  85. }
  86. }
  87. // unsafeReflectValue converts the passed reflect.Value into a one that bypasses
  88. // the typical safety restrictions preventing access to unaddressable and
  89. // unexported data. It works by digging the raw pointer to the underlying
  90. // value out of the protected value and generating a new unprotected (unsafe)
  91. // reflect.Value to it.
  92. //
  93. // This allows us to check for implementations of the Stringer and error
  94. // interfaces to be used for pretty printing ordinarily unaddressable and
  95. // inaccessible values such as unexported struct fields.
  96. func unsafeReflectValue(v reflect.Value) (rv reflect.Value) {
  97. indirects := 1
  98. vt := v.Type()
  99. upv := unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + offsetPtr)
  100. rvf := *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + offsetFlag))
  101. if rvf&flagIndir != 0 {
  102. vt = reflect.PtrTo(v.Type())
  103. indirects++
  104. } else if offsetScalar != 0 {
  105. // The value is in the scalar field when it's not one of the
  106. // reference types.
  107. switch vt.Kind() {
  108. case reflect.Uintptr:
  109. case reflect.Chan:
  110. case reflect.Func:
  111. case reflect.Map:
  112. case reflect.Ptr:
  113. case reflect.UnsafePointer:
  114. default:
  115. upv = unsafe.Pointer(uintptr(unsafe.Pointer(&v)) +
  116. offsetScalar)
  117. }
  118. }
  119. pv := reflect.NewAt(vt, upv)
  120. rv = pv
  121. for i := 0; i < indirects; i++ {
  122. rv = rv.Elem()
  123. }
  124. return rv
  125. }
  126. // Some constants in the form of bytes to avoid string overhead. This mirrors
  127. // the technique used in the fmt package.
  128. var (
  129. panicBytes = []byte("(PANIC=")
  130. plusBytes = []byte("+")
  131. iBytes = []byte("i")
  132. trueBytes = []byte("true")
  133. falseBytes = []byte("false")
  134. interfaceBytes = []byte("(interface {})")
  135. commaNewlineBytes = []byte(",\n")
  136. newlineBytes = []byte("\n")
  137. openBraceBytes = []byte("{")
  138. openBraceNewlineBytes = []byte("{\n")
  139. closeBraceBytes = []byte("}")
  140. asteriskBytes = []byte("*")
  141. colonBytes = []byte(":")
  142. colonSpaceBytes = []byte(": ")
  143. openParenBytes = []byte("(")
  144. closeParenBytes = []byte(")")
  145. spaceBytes = []byte(" ")
  146. pointerChainBytes = []byte("->")
  147. nilAngleBytes = []byte("<nil>")
  148. maxNewlineBytes = []byte("<max depth reached>\n")
  149. maxShortBytes = []byte("<max>")
  150. circularBytes = []byte("<already shown>")
  151. circularShortBytes = []byte("<shown>")
  152. invalidAngleBytes = []byte("<invalid>")
  153. openBracketBytes = []byte("[")
  154. closeBracketBytes = []byte("]")
  155. percentBytes = []byte("%")
  156. precisionBytes = []byte(".")
  157. openAngleBytes = []byte("<")
  158. closeAngleBytes = []byte(">")
  159. openMapBytes = []byte("map[")
  160. closeMapBytes = []byte("]")
  161. lenEqualsBytes = []byte("len=")
  162. capEqualsBytes = []byte("cap=")
  163. )
  164. // hexDigits is used to map a decimal value to a hex digit.
  165. var hexDigits = "0123456789abcdef"
  166. // catchPanic handles any panics that might occur during the handleMethods
  167. // calls.
  168. func catchPanic(w io.Writer, v reflect.Value) {
  169. if err := recover(); err != nil {
  170. w.Write(panicBytes)
  171. fmt.Fprintf(w, "%v", err)
  172. w.Write(closeParenBytes)
  173. }
  174. }
  175. // handleMethods attempts to call the Error and String methods on the underlying
  176. // type the passed reflect.Value represents and outputes the result to Writer w.
  177. //
  178. // It handles panics in any called methods by catching and displaying the error
  179. // as the formatted value.
  180. func handleMethods(cs *ConfigState, w io.Writer, v reflect.Value) (handled bool) {
  181. // We need an interface to check if the type implements the error or
  182. // Stringer interface. However, the reflect package won't give us an
  183. // interface on certain things like unexported struct fields in order
  184. // to enforce visibility rules. We use unsafe to bypass these restrictions
  185. // since this package does not mutate the values.
  186. if !v.CanInterface() {
  187. v = unsafeReflectValue(v)
  188. }
  189. // Choose whether or not to do error and Stringer interface lookups against
  190. // the base type or a pointer to the base type depending on settings.
  191. // Technically calling one of these methods with a pointer receiver can
  192. // mutate the value, however, types which choose to satisify an error or
  193. // Stringer interface with a pointer receiver should not be mutating their
  194. // state inside these interface methods.
  195. var viface interface{}
  196. if !cs.DisablePointerMethods {
  197. if !v.CanAddr() {
  198. v = unsafeReflectValue(v)
  199. }
  200. viface = v.Addr().Interface()
  201. } else {
  202. if v.CanAddr() {
  203. v = v.Addr()
  204. }
  205. viface = v.Interface()
  206. }
  207. // Is it an error or Stringer?
  208. switch iface := viface.(type) {
  209. case error:
  210. defer catchPanic(w, v)
  211. if cs.ContinueOnMethod {
  212. w.Write(openParenBytes)
  213. w.Write([]byte(iface.Error()))
  214. w.Write(closeParenBytes)
  215. w.Write(spaceBytes)
  216. return false
  217. }
  218. w.Write([]byte(iface.Error()))
  219. return true
  220. case fmt.Stringer:
  221. defer catchPanic(w, v)
  222. if cs.ContinueOnMethod {
  223. w.Write(openParenBytes)
  224. w.Write([]byte(iface.String()))
  225. w.Write(closeParenBytes)
  226. w.Write(spaceBytes)
  227. return false
  228. }
  229. w.Write([]byte(iface.String()))
  230. return true
  231. }
  232. return false
  233. }
  234. // printBool outputs a boolean value as true or false to Writer w.
  235. func printBool(w io.Writer, val bool) {
  236. if val {
  237. w.Write(trueBytes)
  238. } else {
  239. w.Write(falseBytes)
  240. }
  241. }
  242. // printInt outputs a signed integer value to Writer w.
  243. func printInt(w io.Writer, val int64, base int) {
  244. w.Write([]byte(strconv.FormatInt(val, base)))
  245. }
  246. // printUint outputs an unsigned integer value to Writer w.
  247. func printUint(w io.Writer, val uint64, base int) {
  248. w.Write([]byte(strconv.FormatUint(val, base)))
  249. }
  250. // printFloat outputs a floating point value using the specified precision,
  251. // which is expected to be 32 or 64bit, to Writer w.
  252. func printFloat(w io.Writer, val float64, precision int) {
  253. w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision)))
  254. }
  255. // printComplex outputs a complex value using the specified float precision
  256. // for the real and imaginary parts to Writer w.
  257. func printComplex(w io.Writer, c complex128, floatPrecision int) {
  258. r := real(c)
  259. w.Write(openParenBytes)
  260. w.Write([]byte(strconv.FormatFloat(r, 'g', -1, floatPrecision)))
  261. i := imag(c)
  262. if i >= 0 {
  263. w.Write(plusBytes)
  264. }
  265. w.Write([]byte(strconv.FormatFloat(i, 'g', -1, floatPrecision)))
  266. w.Write(iBytes)
  267. w.Write(closeParenBytes)
  268. }
  269. // printHexPtr outputs a uintptr formatted as hexidecimal with a leading '0x'
  270. // prefix to Writer w.
  271. func printHexPtr(w io.Writer, p uintptr) {
  272. // Null pointer.
  273. num := uint64(p)
  274. if num == 0 {
  275. w.Write(nilAngleBytes)
  276. return
  277. }
  278. // Max uint64 is 16 bytes in hex + 2 bytes for '0x' prefix
  279. buf := make([]byte, 18)
  280. // It's simpler to construct the hex string right to left.
  281. base := uint64(16)
  282. i := len(buf) - 1
  283. for num >= base {
  284. buf[i] = hexDigits[num%base]
  285. num /= base
  286. i--
  287. }
  288. buf[i] = hexDigits[num]
  289. // Add '0x' prefix.
  290. i--
  291. buf[i] = 'x'
  292. i--
  293. buf[i] = '0'
  294. // Strip unused leading bytes.
  295. buf = buf[i:]
  296. w.Write(buf)
  297. }
  298. // valuesSorter implements sort.Interface to allow a slice of reflect.Value
  299. // elements to be sorted.
  300. type valuesSorter struct {
  301. values []reflect.Value
  302. strings []string // either nil or same len and values
  303. cs *ConfigState
  304. }
  305. // newValuesSorter initializes a valuesSorter instance, which holds a set of
  306. // surrogate keys on which the data should be sorted. It uses flags in
  307. // ConfigState to decide if and how to populate those surrogate keys.
  308. func newValuesSorter(values []reflect.Value, cs *ConfigState) sort.Interface {
  309. vs := &valuesSorter{values: values, cs: cs}
  310. if canSortSimply(vs.values[0].Kind()) {
  311. return vs
  312. }
  313. if !cs.DisableMethods {
  314. vs.strings = make([]string, len(values))
  315. for i := range vs.values {
  316. b := bytes.Buffer{}
  317. if !handleMethods(cs, &b, vs.values[i]) {
  318. vs.strings = nil
  319. break
  320. }
  321. vs.strings[i] = b.String()
  322. }
  323. }
  324. if vs.strings == nil && cs.SpewKeys {
  325. vs.strings = make([]string, len(values))
  326. for i := range vs.values {
  327. vs.strings[i] = Sprintf("%#v", vs.values[i].Interface())
  328. }
  329. }
  330. return vs
  331. }
  332. // canSortSimply tests whether a reflect.Kind is a primitive that can be sorted
  333. // directly, or whether it should be considered for sorting by surrogate keys
  334. // (if the ConfigState allows it).
  335. func canSortSimply(kind reflect.Kind) bool {
  336. // This switch parallels valueSortLess, except for the default case.
  337. switch kind {
  338. case reflect.Bool:
  339. return true
  340. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
  341. return true
  342. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  343. return true
  344. case reflect.Float32, reflect.Float64:
  345. return true
  346. case reflect.String:
  347. return true
  348. case reflect.Uintptr:
  349. return true
  350. case reflect.Array:
  351. return true
  352. }
  353. return false
  354. }
  355. // Len returns the number of values in the slice. It is part of the
  356. // sort.Interface implementation.
  357. func (s *valuesSorter) Len() int {
  358. return len(s.values)
  359. }
  360. // Swap swaps the values at the passed indices. It is part of the
  361. // sort.Interface implementation.
  362. func (s *valuesSorter) Swap(i, j int) {
  363. s.values[i], s.values[j] = s.values[j], s.values[i]
  364. if s.strings != nil {
  365. s.strings[i], s.strings[j] = s.strings[j], s.strings[i]
  366. }
  367. }
  368. // valueSortLess returns whether the first value should sort before the second
  369. // value. It is used by valueSorter.Less as part of the sort.Interface
  370. // implementation.
  371. func valueSortLess(a, b reflect.Value) bool {
  372. switch a.Kind() {
  373. case reflect.Bool:
  374. return !a.Bool() && b.Bool()
  375. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
  376. return a.Int() < b.Int()
  377. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  378. return a.Uint() < b.Uint()
  379. case reflect.Float32, reflect.Float64:
  380. return a.Float() < b.Float()
  381. case reflect.String:
  382. return a.String() < b.String()
  383. case reflect.Uintptr:
  384. return a.Uint() < b.Uint()
  385. case reflect.Array:
  386. // Compare the contents of both arrays.
  387. l := a.Len()
  388. for i := 0; i < l; i++ {
  389. av := a.Index(i)
  390. bv := b.Index(i)
  391. if av.Interface() == bv.Interface() {
  392. continue
  393. }
  394. return valueSortLess(av, bv)
  395. }
  396. }
  397. return a.String() < b.String()
  398. }
  399. // Less returns whether the value at index i should sort before the
  400. // value at index j. It is part of the sort.Interface implementation.
  401. func (s *valuesSorter) Less(i, j int) bool {
  402. if s.strings == nil {
  403. return valueSortLess(s.values[i], s.values[j])
  404. }
  405. return s.strings[i] < s.strings[j]
  406. }
  407. // sortValues is a sort function that handles both native types and any type that
  408. // can be converted to error or Stringer. Other inputs are sorted according to
  409. // their Value.String() value to ensure display stability.
  410. func sortValues(values []reflect.Value, cs *ConfigState) {
  411. if len(values) == 0 {
  412. return
  413. }
  414. sort.Sort(newValuesSorter(values, cs))
  415. }