-
Notifications
You must be signed in to change notification settings - Fork 89
/
gdbutils.py
58 lines (44 loc) · 1.16 KB
/
gdbutils.py
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
import gdb
import re
import codecs
import sys
if sys.version_info[0] >= 3: # Python 3K
stdout = sys.stdout
else:
stdout = codecs.getwriter("utf-8")(sys.stdout)
type_cache = {}
saved_null = False
def newval(typestr, val):
return gdb.Value(val).cast(typ(typestr))
def typ(typestr):
global type_cache
if typestr in type_cache:
return type_cache[typestr]
m = re.match(r"^(\S*)\s*\*$", typestr)
if m is None:
typ = gdb.lookup_type(typestr)
else:
typ = gdb.lookup_type(m.group(1)).pointer()
type_cache[typestr] = typ
return typ
def parse_ptr(val, t):
m = re.match('0[xX][0-9a-fA-F]+', val)
if m:
return newval(t, int(val, 16))
return gdb.parse_and_eval(val)
def err(s):
gdb.write("ERROR: %s\n" % str(s), gdb.STDERR)
def warn(s):
gdb.write("WARNING: %s\n" % str(s), gdb.STDERR)
def out(s):
stdout.write(str(s))
def ptr2int(ptr):
return int(ptr.cast(typ("uintptr_t")))
def null():
global saved_null
if saved_null:
return saved_null
saved_null = newval("void*", 0)
return saved_null
def globalvar(name):
return gdb.lookup_global_symbol(name).value()