/red-system/runtime/debug.reds

http://github.com/dockimbel/Red · Redscript · 143 lines · 128 code · 15 blank · 0 comment · 8 complexity · ce041fe3438f90ae89757f660435acf4 MD5 · raw file

  1. Red/System [
  2. Title: "Red/System runtime debugging functions"
  3. Author: "Nenad Rakocevic"
  4. File: %debug.reds
  5. Rights: "Copyright (C) 2011 Nenad Rakocevic. All rights reserved."
  6. License: {
  7. Distributed under the Boost Software License, Version 1.0.
  8. See https://github.com/dockimbel/Red/blob/master/red-system/runtime/BSL-License.txt
  9. }
  10. ]
  11. __line-record!: alias struct! [ ;-- debug lines records associating code addresses and source lines
  12. address [byte-ptr!] ;-- native code pointer
  13. line [integer!] ;-- source line number
  14. file [integer!] ;-- source file name c-string offset (from first record)
  15. ]
  16. __debug-lines: declare __line-record! ;-- pointer to first debug-lines record (set at link-time)
  17. ;-------------------------------------------
  18. ;-- Calculate line number for a runtime error and print it (internal function).
  19. ;-------------------------------------------
  20. __print-debug-line: func [
  21. address [byte-ptr!] ;-- memory address where the runtime error happened
  22. /local base records
  23. ][
  24. records: __debug-lines
  25. base: as byte-ptr! records
  26. while [records/address < address][ ;-- search for the closest record
  27. records: records + 1
  28. ]
  29. if records/address > address [ ;-- if not an exact match, use the closest lower record
  30. records: records - 1
  31. ]
  32. print [
  33. lf "*** in file: " as-c-string base + records/file
  34. lf "*** at line: " records/line
  35. lf
  36. ]
  37. ]
  38. ;-------------------------------------------
  39. ;-- Print an integer as hex number on screen, limited to n characters
  40. ;-------------------------------------------
  41. prin-hex-chars: func [
  42. i [integer!] ;-- input integer to print
  43. n [integer!] ;-- max number of characters to print (right-aligned)
  44. return: [integer!] ;-- return the input integer (pass-thru)
  45. /local s c d ret
  46. ][
  47. s: "00000000"
  48. if zero? i [
  49. s: "00000000"
  50. print s + (8 - n)
  51. return i
  52. ]
  53. c: 8
  54. ret: i
  55. until [
  56. d: i // 16
  57. if d > 9 [d: d + 7] ;-- 7 = (#"A" - 1) - #"9"
  58. s/c: #"0" + d
  59. i: i >>> 4
  60. c: c - 1
  61. zero? c ;-- iterate on all 8 bytes to overwrite previous values
  62. ]
  63. prin s + (8 - n)
  64. ret
  65. ]
  66. ;-------------------------------------------
  67. ;-- Dump memory on screen in hex format
  68. ;-------------------------------------------
  69. dump-memory: func [
  70. address [byte-ptr!] ;-- memory address where the dump starts
  71. unit [integer!] ;-- size of memory chunks to print in hex format (1 or 4 bytes)
  72. return: [byte-ptr!] ;-- return the pointer (pass-thru)
  73. /local offset ascii i byte int-ptr data-ptr
  74. ][
  75. assert any [unit = 1 unit = 4]
  76. print ["^/Hex dump from: " address "h^/" lf]
  77. offset: 0
  78. ascii: " "
  79. data-ptr: address
  80. until [
  81. print [address ": "]
  82. i: 0
  83. until [
  84. i: i + 1
  85. if unit = 1 [
  86. prin-hex-chars as-integer address/value 2
  87. address: address + 1
  88. print either i = 8 [" "][" "]
  89. ]
  90. if all [unit = 4 zero? (i // 4)][
  91. int-ptr: as int-ptr! address
  92. prin-hex int-ptr/value
  93. address: address + 4
  94. print either i = 8 [" "][" "]
  95. ]
  96. byte: data-ptr/value
  97. ascii/i: either byte < as-byte 32 [
  98. either byte = null-byte [#"."][#"^(FE)"]
  99. ][
  100. byte
  101. ]
  102. data-ptr: data-ptr + 1
  103. i = 16
  104. ]
  105. print [space ascii lf]
  106. offset: offset + 16
  107. offset = 128
  108. ]
  109. address
  110. ]
  111. ;-------------------------------------------
  112. ;-- Dump memory on screen in hex format as array of bytes (handy wrapper on dump-hex)
  113. ;-------------------------------------------
  114. dump-hex: func [
  115. address [byte-ptr!] ;-- memory address where the dump starts
  116. return: [byte-ptr!] ;-- return the pointer (pass-thru)
  117. ][
  118. dump-memory address 1
  119. ]
  120. ;-------------------------------------------
  121. ;-- Dump memory on screen in hex format as array of 32-bit integers (handy wrapper on dump-hex)
  122. ;-------------------------------------------
  123. dump-hex4: func [
  124. address [byte-ptr!] ;-- memory address where the dump starts
  125. return: [byte-ptr!] ;-- return the pointer (pass-thru)
  126. ][
  127. dump-memory address 4
  128. ]