-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathlibpy-gdb.py
50 lines (38 loc) · 1.12 KB
/
libpy-gdb.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
import re
import gdb
import numpy as np
def pretty_printer(cls):
gdb.pretty_printers.append(cls.maybe_construct)
return cls
@pretty_printer
class Datetime64:
_pattern = re.compile(
r'^py::datetime64<std::chrono::duration<long,'
r' std::ratio<(\d+), (\d+)> > >$'
)
_units = {
(1, 1000000000): 'ns',
(1, 1000000): 'ms',
(1, 1000): 'us',
(1, 1): 's',
(60, 1): 'm',
(60 * 60, 1): 'h',
(60 * 60 * 24, 1): 'D',
}
def __init__(self, val, count, unit):
self.val = val
self.count = count
self.unit = unit
@classmethod
def maybe_construct(cls, val):
underlying = gdb.types.get_basic_type(val.type)
match = cls._pattern.match(str(underlying))
if match is None:
return None
num = int(match[1])
den = int(match[2])
return cls(val, int(val['m_value']['__r']), cls._units[num, den])
def children(self):
yield 'm_value', str(np.datetime64(self.count, self.unit))
def to_string(self):
return f'py::datetime64<py::chrono::{self.unit}>'