-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.nim
154 lines (141 loc) · 3.26 KB
/
debugger.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
## =======================
## Native Debugger Support
## =======================
##
## Some aids for debugging Nim projects with a native debugger.
##
##
## Design
## ======
##
## * each debugger has an extension
## * some extensions can be embedded into the debuggee
## * others have to be built as separate files that are loaded by a debugger
## * a debugger ext calls debugger procs inside the debuggee
## * embedded debugger procs can eval expressions in the debugger
##
## Debuggers
## =========
##
## LLDB
## ----
##
##
##
## GDB
## ---
##
##
##
## See also
## --------
##
##
##
## TODO
## ----
##
## *
## *
## *
##
import std/[re]
from std/importutils import privateAccess
import std/typeinfo {.all.}
import pkg/nimpy
let nimTypeRe = re"^([A-Za-z0-9]+)_[A-Za-z0-9]*_+[A-Za-z0-9]*$"
proc debuggerInit {.exportc.} =
echo "Nim Debugger Runtime ENABLED"
proc debuggerTypeNameFromNimNode(node: int): string {.exportpy.} =
let n = cast[ptr TNimNode](node)
privateAccess(typeof(n)) # enables private access in this scope
$n.name
proc debuggerTypeNameFromMangled(mangled: string): string {.exportpy.} =
case mangled
of "NI":
return "system.int"
of "NI8":
return "int8"
of "NI16":
return "int16"
of "NI32":
return "int32"
of "NI64":
return "int64"
of "NU":
return "uint"
of "NU8":
return "uint8"
of "NU16":
return "uint16"
of "NU32":
return "uint32"
of "NU64":
return "uint64"
of "NF":
return "float"
of "NF32":
return "float32"
of "NF64":
return "float64"
of "NIM_CHAR":
return "char"
of "NCSTRING":
return "cstring"
of "NimStringV2", "NimStringDesc":
return "string"
of "_Bool", "NIM_BOOL": # see "bool types" in lib/system/nimbase.h
return "bool"
else:
var matches: array[1, string]
if match($mangled, nimTypeRe, matches, 0):
case matches[0]
of "tyObject":
# var a = toAny(cast[var RootObj](address))
# for f in a.fields:
# echo $f
return "object"
# else:
# of "tySequence": return repr(cast[openArray](address)).cstring
else:
return mangled
proc debuggerRepr(kind: cstring, address: pointer): cstring {.exportc.} =
var matches: array[3, string]
if match($kind, nimTypeRe, matches, 0):
case matches[0]
of "tyObject":
# var a = toAny(cast[var RootObj](address))
# for f in a.fields:
# echo $f
echo matches
return "repr(cast[RootObj](address)).cstring"
of "NimStringDesc":
echo matches
return "repr(cast[string](address)).cstring"
else:
echo matches
# of "tySequence": return repr(cast[openArray](address)).cstring
else:
return kind
proc debuggerHint(kind: cstring): cstring {.exportc.} =
var matches: array[2, string]
if match($kind, nimTypeRe, matches, 0):
return matches[0].cstring
else:
echo matches
return kind
#[ const debuggerSection = """
__asm__ (
".pushsection __TEXT, debugger\n"
".byte 4\n" // python text
".byte 0\n"
".popsection\n"
);
"""
{.emit: debuggerSection.} ]#
#[ from ast import PNode, safeLen
from renderer import renderTree
proc debuggerRenderTree(node: PNode): string {.exportc.} =
renderTree node
func debuggerNodeSonsLen(node: PNode): int {.exportc.} =
safeLen node ]#