common.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. "fmt"
  19. "io"
  20. "reflect"
  21. "sort"
  22. "strconv"
  23. "unsafe"
  24. )
  25. // offsetPtr, offsetScalar, and offsetFlag are the offsets for the internal
  26. // reflect.Value fields.
  27. var offsetPtr, offsetScalar, offsetFlag uintptr
  28. // reflectValueOld mirrors the struct layout of the reflect package Value type
  29. // before golang commit ecccf07e7f9d.
  30. var reflectValueOld struct {
  31. typ unsafe.Pointer
  32. val unsafe.Pointer
  33. flag uintptr
  34. }
  35. // reflectValueNew mirrors the struct layout of the reflect package Value type
  36. // after golang commit ecccf07e7f9d.
  37. var reflectValueNew struct {
  38. typ unsafe.Pointer
  39. ptr unsafe.Pointer
  40. scalar uintptr
  41. flag uintptr
  42. }
  43. func init() {
  44. // Older versions of reflect.Value stored small integers directly in the
  45. // ptr field (which is named val in the older versions). Newer versions
  46. // added a new field named scalar for this purpose which unfortuantely
  47. // comes before the flag field. Further the new field is before the
  48. // flag field, so the offset of the flag field is different as well.
  49. // This code constructs a new reflect.Value from a known small integer
  50. // and checks if the val field within it matches. When it matches, the
  51. // old style reflect.Value is being used. Otherwise it's the new style.
  52. v := 0xf00
  53. vv := reflect.ValueOf(v)
  54. upv := unsafe.Pointer(uintptr(unsafe.Pointer(&vv)) +
  55. unsafe.Offsetof(reflectValueOld.val))
  56. // Assume the old style by default.
  57. offsetPtr = unsafe.Offsetof(reflectValueOld.val)
  58. offsetScalar = 0
  59. offsetFlag = unsafe.Offsetof(reflectValueOld.flag)
  60. // Use the new style offsets if the ptr field doesn't match the value
  61. // since it must be in the new scalar field.
  62. if int(*(*uintptr)(upv)) != v {
  63. offsetPtr = unsafe.Offsetof(reflectValueNew.ptr)
  64. offsetScalar = unsafe.Offsetof(reflectValueNew.scalar)
  65. offsetFlag = unsafe.Offsetof(reflectValueNew.flag)
  66. }
  67. }
  68. // flagIndir indicates whether the value field of a reflect.Value is the actual
  69. // data or a pointer to the data.
  70. const flagIndir = 1 << 1
  71. // unsafeReflectValue converts the passed reflect.Value into a one that bypasses
  72. // the typical safety restrictions preventing access to unaddressable and
  73. // unexported data. It works by digging the raw pointer to the underlying
  74. // value out of the protected value and generating a new unprotected (unsafe)
  75. // reflect.Value to it.
  76. //
  77. // This allows us to check for implementations of the Stringer and error
  78. // interfaces to be used for pretty printing ordinarily unaddressable and
  79. // inaccessible values such as unexported struct fields.
  80. func unsafeReflectValue(v reflect.Value) (rv reflect.Value) {
  81. indirects := 1
  82. vt := v.Type()
  83. upv := unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + offsetPtr)
  84. rvf := *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + offsetFlag))
  85. if rvf&flagIndir != 0 {
  86. vt = reflect.PtrTo(v.Type())
  87. indirects++
  88. } else if offsetScalar != 0 {
  89. upv = unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + offsetScalar)
  90. }
  91. pv := reflect.NewAt(vt, upv)
  92. rv = pv
  93. for i := 0; i < indirects; i++ {
  94. rv = rv.Elem()
  95. }
  96. return rv
  97. }
  98. // Some constants in the form of bytes to avoid string overhead. This mirrors
  99. // the technique used in the fmt package.
  100. var (
  101. panicBytes = []byte("(PANIC=")
  102. plusBytes = []byte("+")
  103. iBytes = []byte("i")
  104. trueBytes = []byte("true")
  105. falseBytes = []byte("false")
  106. interfaceBytes = []byte("(interface {})")
  107. commaNewlineBytes = []byte(",\n")
  108. newlineBytes = []byte("\n")
  109. openBraceBytes = []byte("{")
  110. openBraceNewlineBytes = []byte("{\n")
  111. closeBraceBytes = []byte("}")
  112. asteriskBytes = []byte("*")
  113. colonBytes = []byte(":")
  114. colonSpaceBytes = []byte(": ")
  115. openParenBytes = []byte("(")
  116. closeParenBytes = []byte(")")
  117. spaceBytes = []byte(" ")
  118. pointerChainBytes = []byte("->")
  119. nilAngleBytes = []byte("<nil>")
  120. maxNewlineBytes = []byte("<max depth reached>\n")
  121. maxShortBytes = []byte("<max>")
  122. circularBytes = []byte("<already shown>")
  123. circularShortBytes = []byte("<shown>")
  124. invalidAngleBytes = []byte("<invalid>")
  125. openBracketBytes = []byte("[")
  126. closeBracketBytes = []byte("]")
  127. percentBytes = []byte("%")
  128. precisionBytes = []byte(".")
  129. openAngleBytes = []byte("<")
  130. closeAngleBytes = []byte(">")
  131. openMapBytes = []byte("map[")
  132. closeMapBytes = []byte("]")
  133. lenEqualsBytes = []byte("len=")
  134. capEqualsBytes = []byte("cap=")
  135. )
  136. // hexDigits is used to map a decimal value to a hex digit.
  137. var hexDigits = "0123456789abcdef"
  138. // catchPanic handles any panics that might occur during the handleMethods
  139. // calls.
  140. func catchPanic(w io.Writer, v reflect.Value) {
  141. if err := recover(); err != nil {
  142. w.Write(panicBytes)
  143. fmt.Fprintf(w, "%v", err)
  144. w.Write(closeParenBytes)
  145. }
  146. }
  147. // handleMethods attempts to call the Error and String methods on the underlying
  148. // type the passed reflect.Value represents and outputes the result to Writer w.
  149. //
  150. // It handles panics in any called methods by catching and displaying the error
  151. // as the formatted value.
  152. func handleMethods(cs *ConfigState, w io.Writer, v reflect.Value) (handled bool) {
  153. // We need an interface to check if the type implements the error or
  154. // Stringer interface. However, the reflect package won't give us an
  155. // interface on certain things like unexported struct fields in order
  156. // to enforce visibility rules. We use unsafe to bypass these restrictions
  157. // since this package does not mutate the values.
  158. if !v.CanInterface() {
  159. v = unsafeReflectValue(v)
  160. }
  161. // Choose whether or not to do error and Stringer interface lookups against
  162. // the base type or a pointer to the base type depending on settings.
  163. // Technically calling one of these methods with a pointer receiver can
  164. // mutate the value, however, types which choose to satisify an error or
  165. // Stringer interface with a pointer receiver should not be mutating their
  166. // state inside these interface methods.
  167. var viface interface{}
  168. if !cs.DisablePointerMethods {
  169. if !v.CanAddr() {
  170. v = unsafeReflectValue(v)
  171. }
  172. viface = v.Addr().Interface()
  173. } else {
  174. if v.CanAddr() {
  175. v = v.Addr()
  176. }
  177. viface = v.Interface()
  178. }
  179. // Is it an error or Stringer?
  180. switch iface := viface.(type) {
  181. case error:
  182. defer catchPanic(w, v)
  183. if cs.ContinueOnMethod {
  184. w.Write(openParenBytes)
  185. w.Write([]byte(iface.Error()))
  186. w.Write(closeParenBytes)
  187. w.Write(spaceBytes)
  188. return false
  189. }
  190. w.Write([]byte(iface.Error()))
  191. return true
  192. case fmt.Stringer:
  193. defer catchPanic(w, v)
  194. if cs.ContinueOnMethod {
  195. w.Write(openParenBytes)
  196. w.Write([]byte(iface.String()))
  197. w.Write(closeParenBytes)
  198. w.Write(spaceBytes)
  199. return false
  200. }
  201. w.Write([]byte(iface.String()))
  202. return true
  203. }
  204. return false
  205. }
  206. // printBool outputs a boolean value as true or false to Writer w.
  207. func printBool(w io.Writer, val bool) {
  208. if val {
  209. w.Write(trueBytes)
  210. } else {
  211. w.Write(falseBytes)
  212. }
  213. }
  214. // printInt outputs a signed integer value to Writer w.
  215. func printInt(w io.Writer, val int64, base int) {
  216. w.Write([]byte(strconv.FormatInt(val, base)))
  217. }
  218. // printUint outputs an unsigned integer value to Writer w.
  219. func printUint(w io.Writer, val uint64, base int) {
  220. w.Write([]byte(strconv.FormatUint(val, base)))
  221. }
  222. // printFloat outputs a floating point value using the specified precision,
  223. // which is expected to be 32 or 64bit, to Writer w.
  224. func printFloat(w io.Writer, val float64, precision int) {
  225. w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision)))
  226. }
  227. // printComplex outputs a complex value using the specified float precision
  228. // for the real and imaginary parts to Writer w.
  229. func printComplex(w io.Writer, c complex128, floatPrecision int) {
  230. r := real(c)
  231. w.Write(openParenBytes)
  232. w.Write([]byte(strconv.FormatFloat(r, 'g', -1, floatPrecision)))
  233. i := imag(c)
  234. if i >= 0 {
  235. w.Write(plusBytes)
  236. }
  237. w.Write([]byte(strconv.FormatFloat(i, 'g', -1, floatPrecision)))
  238. w.Write(iBytes)
  239. w.Write(closeParenBytes)
  240. }
  241. // printHexPtr outputs a uintptr formatted as hexidecimal with a leading '0x'
  242. // prefix to Writer w.
  243. func printHexPtr(w io.Writer, p uintptr) {
  244. // Null pointer.
  245. num := uint64(p)
  246. if num == 0 {
  247. w.Write(nilAngleBytes)
  248. return
  249. }
  250. // Max uint64 is 16 bytes in hex + 2 bytes for '0x' prefix
  251. buf := make([]byte, 18)
  252. // It's simpler to construct the hex string right to left.
  253. base := uint64(16)
  254. i := len(buf) - 1
  255. for num >= base {
  256. buf[i] = hexDigits[num%base]
  257. num /= base
  258. i--
  259. }
  260. buf[i] = hexDigits[num]
  261. // Add '0x' prefix.
  262. i--
  263. buf[i] = 'x'
  264. i--
  265. buf[i] = '0'
  266. // Strip unused leading bytes.
  267. buf = buf[i:]
  268. w.Write(buf)
  269. }
  270. // valuesSorter implements sort.Interface to allow a slice of reflect.Value
  271. // elements to be sorted.
  272. type valuesSorter struct {
  273. values []reflect.Value
  274. }
  275. // Len returns the number of values in the slice. It is part of the
  276. // sort.Interface implementation.
  277. func (s *valuesSorter) Len() int {
  278. return len(s.values)
  279. }
  280. // Swap swaps the values at the passed indices. It is part of the
  281. // sort.Interface implementation.
  282. func (s *valuesSorter) Swap(i, j int) {
  283. s.values[i], s.values[j] = s.values[j], s.values[i]
  284. }
  285. // Less returns whether the value at index i should sort before the
  286. // value at index j. It is part of the sort.Interface implementation.
  287. func (s *valuesSorter) Less(i, j int) bool {
  288. switch s.values[i].Kind() {
  289. case reflect.Bool:
  290. return !s.values[i].Bool() && s.values[j].Bool()
  291. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
  292. return s.values[i].Int() < s.values[j].Int()
  293. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  294. return s.values[i].Uint() < s.values[j].Uint()
  295. case reflect.Float32, reflect.Float64:
  296. return s.values[i].Float() < s.values[j].Float()
  297. case reflect.String:
  298. return s.values[i].String() < s.values[j].String()
  299. case reflect.Uintptr:
  300. return s.values[i].Uint() < s.values[j].Uint()
  301. }
  302. return s.values[i].String() < s.values[j].String()
  303. }
  304. // sortValues is a generic sort function for native types: int, uint, bool,
  305. // string and uintptr. Other inputs are sorted according to their
  306. // Value.String() value to ensure display stability.
  307. func sortValues(values []reflect.Value) {
  308. if len(values) == 0 {
  309. return
  310. }
  311. sort.Sort(&valuesSorter{values})
  312. }