debugutils.nim 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ##[
  2. Utilities to help with debugging nim compiler.
  3. Experimental API, subject to change.
  4. ]##
  5. #[
  6. ## example
  7. useful debugging flags:
  8. --stacktrace -d:debug -d:nimDebugUtils
  9. nim c -o:bin/nim_temp --stacktrace -d:debug -d:nimDebugUtils compiler/nim
  10. ## future work
  11. * expose and improve astalgo.debug, replacing it by std/prettyprints,
  12. refs https://github.com/nim-lang/RFCs/issues/385
  13. ]#
  14. import options
  15. import std/wrapnils
  16. export wrapnils
  17. # allows using things like: `?.n.sym.typ.len`
  18. import std/stackframes
  19. export stackframes
  20. # allows using things like: `setFrameMsg c.config$n.info & " " & $n.kind`
  21. # which doesn't log, but augments stacktrace with side channel information
  22. var conf0: ConfigRef
  23. proc onNewConfigRef*(conf: ConfigRef) {.inline.} =
  24. ## Caches `conf`, which can be retrieved with `getConfigRef`.
  25. ## This avoids having to forward `conf` all the way down the call chain to
  26. ## procs that need it during a debugging session.
  27. conf0 = conf
  28. proc getConfigRef*(): ConfigRef =
  29. ## nil, if -d:nimDebugUtils wasn't specified
  30. result = conf0
  31. proc isCompilerDebug*(): bool =
  32. ##[
  33. Provides a simple way for user code to enable/disable logging in the compiler
  34. in a granular way. This can then be used in the compiler as follows:
  35. ```nim
  36. if isCompilerDebug():
  37. echo ?.n.sym.typ.len
  38. ```
  39. ]##
  40. runnableExamples:
  41. proc main =
  42. echo 2
  43. {.define(nimCompilerDebug).}
  44. echo 3.5 # code section in which `isCompilerDebug` will be true
  45. {.undef(nimCompilerDebug).}
  46. echo 'x'
  47. conf0.isDefined("nimCompilerDebug")
  48. proc enteringDebugSection*() {.exportc, dynlib.} =
  49. ## Provides a way for native debuggers to enable breakpoints, watchpoints, etc
  50. ## when code of interest is being compiled.
  51. ##
  52. ## Set your debugger to break on entering `nimCompilerIsEnteringDebugSection`
  53. ## and then execute a desired command.
  54. discard
  55. proc exitingDebugSection*() {.exportc, dynlib.} =
  56. ## Provides a way for native debuggers to disable breakpoints, watchpoints, etc
  57. ## when code of interest is no longer being compiled.
  58. ##
  59. ## Set your debugger to break on entering `exitingDebugSection`
  60. ## and then execute a desired command.
  61. discard