debug.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 (hoot primitives)
  23. (hoot match))
  24. (define (1+ x) (%+ x 1))
  25. (define (1- x) (%- x 1))
  26. (define (- x y) (%- x y))
  27. (define (<= x y) (%<= x y))
  28. (define dprint
  29. (case-lambda
  30. ((message)
  31. (%inline-wasm
  32. '(func (param $str (ref string))
  33. (call $debug-str (local.get $str)))
  34. message))
  35. ((message val)
  36. (%inline-wasm
  37. '(func (param $str (ref string)) (param $val (ref eq))
  38. (call $debug-str-scm (local.get $str) (local.get $val)))
  39. message val))))
  40. (define* (pk v . v*)
  41. (let lp ((v v) (v* v*))
  42. (match v*
  43. (()
  44. (dprint "pkv" v)
  45. v)
  46. ((v* . v**)
  47. (dprint "pk_" v)
  48. (lp v* v**)))))
  49. (define (backtrace)
  50. (define (scm-sp)
  51. (%inline-wasm
  52. '(func (result (ref eq))
  53. (ref.i31 (i32.shl (global.get $scm-sp) (i32.const 1))))))
  54. (define (raw-sp)
  55. (%inline-wasm
  56. '(func (result (ref eq))
  57. (ref.i31 (i32.shl (global.get $raw-sp) (i32.const 1))))))
  58. (define (ret-sp)
  59. (%inline-wasm
  60. '(func (result (ref eq))
  61. (ref.i31 (i32.shl (global.get $ret-sp) (i32.const 1))))))
  62. (define (dyn-sp)
  63. (%inline-wasm
  64. '(func (result (ref eq))
  65. (ref.i31 (i32.shl (global.get $dyn-sp) (i32.const 1))))))
  66. (define (scm-ref n)
  67. (%inline-wasm
  68. '(func (param $n (ref i31))
  69. (result (ref eq))
  70. (ref.as_non_null
  71. (table.get $scm-stack
  72. (i32.shr_s (i31.get_s (local.get $n))
  73. (i32.const 1)))))
  74. n))
  75. (define (raw-ref n)
  76. (%inline-wasm
  77. '(func (param $n (ref i31))
  78. (result (ref eq))
  79. (ref.i31
  80. (i32.shl
  81. (i32.load8_s $raw-stack
  82. (i32.shr_s (i31.get_s (local.get $n))
  83. (i32.const 1)))
  84. (i32.const 1))))
  85. n))
  86. (let ((scm-sp (scm-sp))
  87. (raw-sp (raw-sp))
  88. (ret-sp (ret-sp))
  89. (dyn-sp (dyn-sp)))
  90. (dprint "scm backtrace" scm-sp)
  91. (let lp ((i 1))
  92. (when (<= 0 (- scm-sp i))
  93. (dprint "scm" (scm-ref (- scm-sp i)))
  94. (lp (1+ i))))
  95. (dprint "raw backtrace" raw-sp)
  96. (let lp ((i 1))
  97. (when (<= 0 (- raw-sp i))
  98. (dprint "raw" (raw-ref (- raw-sp i)))
  99. (lp (1+ i))))
  100. (dprint "ret stack height" ret-sp)
  101. (dprint "dyn stack height" dyn-sp)
  102. (dprint ""))))