debug.scm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ;;; Debugging utilities
  2. ;;; Copyright (C) 2024 Igalia, S.L.
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Backtraces and so on.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot debug)
  21. (export dprint pk backtrace)
  22. (import (only (hoot primitives) %+ %- %<=)
  23. (hoot inline-wasm)
  24. (hoot match)
  25. (hoot syntax))
  26. (define (1+ x) (%+ x 1))
  27. (define (1- x) (%- x 1))
  28. (define (- x y) (%- x y))
  29. (define (<= x y) (%<= x y))
  30. (define dprint
  31. (case-lambda
  32. ((message)
  33. (%inline-wasm
  34. '(func (param $str (ref string))
  35. (call $debug-str (local.get $str)))
  36. message))
  37. ((message val)
  38. (%inline-wasm
  39. '(func (param $str (ref string)) (param $val (ref eq))
  40. (call $debug-str-scm (local.get $str) (local.get $val)))
  41. message val))))
  42. (define* (pk v . v*)
  43. (let lp ((v v) (v* v*))
  44. (match v*
  45. (()
  46. (dprint "pkv" v)
  47. v)
  48. ((v* . v**)
  49. (dprint "pk_" v)
  50. (lp v* v**)))))
  51. (define (backtrace)
  52. (define (scm-sp)
  53. (%inline-wasm
  54. '(func (result (ref eq))
  55. (ref.i31 (i32.shl (global.get $scm-sp) (i32.const 1))))))
  56. (define (raw-sp)
  57. (%inline-wasm
  58. '(func (result (ref eq))
  59. (ref.i31 (i32.shl (global.get $raw-sp) (i32.const 1))))))
  60. (define (ret-sp)
  61. (%inline-wasm
  62. '(func (result (ref eq))
  63. (ref.i31 (i32.shl (global.get $ret-sp) (i32.const 1))))))
  64. (define (dyn-sp)
  65. (%inline-wasm
  66. '(func (result (ref eq))
  67. (ref.i31 (i32.shl (global.get $dyn-sp) (i32.const 1))))))
  68. (define (scm-ref n)
  69. (%inline-wasm
  70. '(func (param $n (ref i31))
  71. (result (ref eq))
  72. (ref.as_non_null
  73. (table.get $scm-stack
  74. (i32.shr_s (i31.get_s (local.get $n))
  75. (i32.const 1)))))
  76. n))
  77. (define (raw-ref n)
  78. (%inline-wasm
  79. '(func (param $n (ref i31))
  80. (result (ref eq))
  81. (ref.i31
  82. (i32.shl
  83. (i32.load8_s $raw-stack
  84. (i32.shr_s (i31.get_s (local.get $n))
  85. (i32.const 1)))
  86. (i32.const 1))))
  87. n))
  88. (let ((scm-sp (scm-sp))
  89. (raw-sp (raw-sp))
  90. (ret-sp (ret-sp))
  91. (dyn-sp (dyn-sp)))
  92. (dprint "scm backtrace" scm-sp)
  93. (let lp ((i 1))
  94. (when (<= 0 (- scm-sp i))
  95. (dprint "scm" (scm-ref (- scm-sp i)))
  96. (lp (1+ i))))
  97. (dprint "raw backtrace" raw-sp)
  98. (let lp ((i 1))
  99. (when (<= 0 (- raw-sp i))
  100. (dprint "raw" (raw-ref (- raw-sp i)))
  101. (lp (1+ i))))
  102. (dprint "ret stack height" ret-sp)
  103. (dprint "dyn stack height" dyn-sp)
  104. (dprint ""))))