File: symgroup.py

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (104 lines) | stat: -rw-r--r-- 3,714 bytes parent folder | download | duplicates (12)
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
# DExTer : Debugging Experience Tester
# ~~~~~~   ~         ~~         ~   ~~
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

from collections import namedtuple
from ctypes import *
from functools import partial

from .utils import *

Symbol = namedtuple("Symbol", ["num", "name", "type", "value"])


class IDebugSymbolGroup2(Structure):
    pass


class IDebugSymbolGroup2Vtbl(Structure):
    wrp = partial(WINFUNCTYPE, c_long, POINTER(IDebugSymbolGroup2))
    ids_getnumbersymbols = wrp(c_ulong_p)
    ids_getsymbolname = wrp(c_ulong, c_char_p, c_ulong, c_ulong_p)
    ids_getsymboltypename = wrp(c_ulong, c_char_p, c_ulong, c_ulong_p)
    ids_getsymbolvaluetext = wrp(c_ulong, c_char_p, c_ulong, c_ulong_p)
    _fields_ = [
        ("QueryInterface", c_void_p),
        ("AddRef", c_void_p),
        ("Release", c_void_p),
        ("GetNumberSymbols", ids_getnumbersymbols),
        ("AddSymbol", c_void_p),
        ("RemoveSymbolByName", c_void_p),
        ("RemoveSymbolByIndex", c_void_p),
        ("GetSymbolName", ids_getsymbolname),
        ("GetSymbolParameters", c_void_p),
        ("ExpandSymbol", c_void_p),
        ("OutputSymbols", c_void_p),
        ("WriteSymbol", c_void_p),
        ("OutputAsType", c_void_p),
        ("AddSymbolWide", c_void_p),
        ("RemoveSymbolByNameWide", c_void_p),
        ("GetSymbolNameWide", c_void_p),
        ("WritesymbolWide", c_void_p),
        ("OutputAsTypeWide", c_void_p),
        ("GetSymbolTypeName", ids_getsymboltypename),
        ("GetSymbolTypeNameWide", c_void_p),
        ("GetSymbolSize", c_void_p),
        ("GetSymbolOffset", c_void_p),
        ("GetSymbolRegister", c_void_p),
        ("GetSymbolValueText", ids_getsymbolvaluetext),
        ("GetSymbolValueTextWide", c_void_p),
        ("GetSymbolEntryInformation", c_void_p),
    ]


IDebugSymbolGroup2._fields_ = [("lpVtbl", POINTER(IDebugSymbolGroup2Vtbl))]


class SymbolGroup(object):
    def __init__(self, symgroup):
        self.symgroup = symgroup.contents
        self.vt = self.symgroup.lpVtbl.contents
        self.ulong = c_ulong()

    def GetNumberSymbols(self):
        res = self.vt.GetNumberSymbols(self.symgroup, byref(self.ulong))
        aborter(res, "GetNumberSymbols")
        return self.ulong.value

    def GetSymbolName(self, idx):
        buf = create_string_buffer(256)
        res = self.vt.GetSymbolName(self.symgroup, idx, buf, 255, byref(self.ulong))
        aborter(res, "GetSymbolName")
        thelen = self.ulong.value
        return string_at(buf).decode("ascii")

    def GetSymbolTypeName(self, idx):
        buf = create_string_buffer(256)
        res = self.vt.GetSymbolTypeName(self.symgroup, idx, buf, 255, byref(self.ulong))
        aborter(res, "GetSymbolTypeName")
        thelen = self.ulong.value
        return string_at(buf).decode("ascii")

    def GetSymbolValueText(self, idx, handleserror=False):
        buf = create_string_buffer(256)
        res = self.vt.GetSymbolValueText(
            self.symgroup, idx, buf, 255, byref(self.ulong)
        )
        if res != 0 and handleserror:
            return None
        aborter(res, "GetSymbolTypeName")
        thelen = self.ulong.value
        return string_at(buf).decode("ascii")

    def get_symbol(self, idx):
        name = self.GetSymbolName(idx)
        thetype = self.GetSymbolTypeName(idx)
        value = self.GetSymbolValueText(idx)
        return Symbol(idx, name, thetype, value)

    def get_all_symbols(self):
        num_syms = self.GetNumberSymbols()
        return list(map(self.get_symbol, list(range(num_syms))))