Debian Codegraph @

libdrgn/python/platform.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include "drgnpy.h"
5
6 PyObject *Platform_wrap(const struct drgn_platform *platform)
7 {
8 struct drgn_error *err;
9 struct drgn_platform *tmp;
10 err = drgn_platform_create(drgn_platform_arch(platform),
11 drgn_platform_flags(platform),
12 &tmp);
13 if (err)
14 return set_drgn_error(err);
15 Platform *ret = call_tp_alloc(Platform);
16 if (!ret)
17 return NULL;
18 ret->platform = tmp;
19 return (PyObject *)ret;
20 }
21
22 static Platform *Platform_new(PyTypeObject *subtype, PyObject *args,
23 PyObject *kwds)
24 {
25 static char *keywords[] = {"arch", "flags", NULL};
26 struct enum_arg arch = { .type = Architecture_class, };
27 struct enum_arg flags = {
28 .type = PlatformFlags_class,
29 .value = DRGN_PLATFORM_DEFAULT_FLAGS,
30 .allow_none = true,
31 };
32 struct drgn_error *err;
33 struct drgn_platform *platform;
34 Platform *ret;
35
36 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&:Platform", keywords,
37 enum_converter, &arch, enum_converter,
38 &flags))
39 return NULL;
40
41 err = drgn_platform_create(arch.value, flags.value, &platform);
42 if (err)
43 return set_drgn_error(err);
44 ret = (Platform *)subtype->tp_alloc(subtype, 0);
45 if (!ret) {
46 drgn_platform_destroy(platform);
47 return NULL;
48 }
49 ret->platform = platform;
50 return ret;
51 }
52
53 static void Platform_dealloc(Platform *self)
54 {
55 drgn_platform_destroy(self->platform);
56 Py_TYPE(self)->tp_free((PyObject *)self);
57 }
58
59 static PyObject *Platform_richcompare(Platform *self, PyObject *other, int op)
60 {
61 if (!PyObject_TypeCheck(other, &Platform_type) ||
62 (op != Py_EQ && op != Py_NE))
63 Py_RETURN_NOTIMPLEMENTED;
64 bool ret = drgn_platform_eq(self->platform,
65 ((Platform *)other)->platform);
66 if (op == Py_NE)
67 ret = !ret;
68 Py_RETURN_BOOL(ret);
69 }
70
71 static PyObject *Platform_get_arch(Platform *self, void *arg)
72 {
73 return PyObject_CallFunction(Architecture_class, "k",
74 (unsigned long)drgn_platform_arch(self->platform));
75 }
76
77 static PyObject *Platform_get_flags(Platform *self, void *arg)
78 {
79 return PyObject_CallFunction(PlatformFlags_class, "k",
80 (unsigned long)drgn_platform_flags(self->platform));
81 }
82
83 static PyObject *Platform_get_registers(Platform *self, void *arg)
84 {
85 size_t num_registers = drgn_platform_num_registers(self->platform);
86 _cleanup_pydecref_ PyObject *tuple = PyTuple_New(num_registers);
87 if (!tuple)
88 return NULL;
89 for (size_t i = 0; i < num_registers; i++) {
90 const struct drgn_register *reg =
91 drgn_platform_register(self->platform, i);
92 Register *item = call_tp_alloc(Register);
93 if (!item)
94 return NULL;
95 item->reg = reg;
96 PyTuple_SET_ITEM(tuple, i, (PyObject *)item);
97 }
98 return_ptr(tuple);
99 }
100
101 static PyObject *Platform_repr(Platform *self)
102 {
103 _cleanup_pydecref_ PyObject *arch_obj = Platform_get_arch(self, NULL);
104 if (!arch_obj)
105 return NULL;
106 _cleanup_pydecref_ PyObject *flags_obj = Platform_get_flags(self, NULL);
107 if (!flags_obj)
108 return NULL;
109 return PyUnicode_FromFormat("Platform(%R, %R)", arch_obj, flags_obj);
110 }
111
112 static PyGetSetDef Platform_getset[] = {
113 {"arch", (getter)Platform_get_arch, NULL, drgn_Platform_arch_DOC},
114 {"flags", (getter)Platform_get_flags, NULL, drgn_Platform_flags_DOC},
115 {"registers", (getter)Platform_get_registers, NULL,
116 drgn_Platform_registers_DOC},
117 {},
118 };
119
120 PyTypeObject Platform_type = {
121 PyVarObject_HEAD_INIT(NULL, 0)
122 .tp_name = "_drgn.Platform",
123 .tp_basicsize = sizeof(Platform),
124 .tp_dealloc = (destructor)Platform_dealloc,
125 .tp_repr = (reprfunc)Platform_repr,
126 // Doesn't reference any objects, no GC needed.
127 .tp_flags = Py_TPFLAGS_DEFAULT,
128 .tp_doc = drgn_Platform_DOC,
129 .tp_richcompare = (richcmpfunc)Platform_richcompare,
130 .tp_getset = Platform_getset,
131 .tp_new = (newfunc)Platform_new,
132 };
133
134 static PyObject *Register_richcompare(Register *self, PyObject *other, int op)
135 {
136 if (!PyObject_TypeCheck(other, &Register_type) ||
137 (op != Py_EQ && op != Py_NE))
138 Py_RETURN_NOTIMPLEMENTED;
139 bool ret = self->reg == ((Register *)other)->reg;
140 if (op == Py_NE)
141 ret = !ret;
142 Py_RETURN_BOOL(ret);
143 }
144
145 static PyObject *Register_get_names(Register *self, void *arg)
146 {
147 size_t num_names;
148 const char * const *names = drgn_register_names(self->reg, &num_names);
149 _cleanup_pydecref_ PyObject *ret = PyTuple_New(num_names);
150 for (size_t i = 0; i < num_names; i++) {
151 PyObject *item = PyUnicode_FromString(names[i]);
152 if (!item)
153 return NULL;
154 PyTuple_SET_ITEM(ret, i, item);
155 }
156 return_ptr(ret);
157 }
158
159 static PyObject *Register_repr(Register *self)
160 {
161 _cleanup_pydecref_ PyObject *names_obj = Register_get_names(self, NULL);
162 if (!names_obj)
163 return NULL;
164 return PyUnicode_FromFormat("Register(%R)", names_obj);
165 }
166
167 static PyGetSetDef Register_getset[] = {
168 {"names", (getter)Register_get_names, NULL, drgn_Register_names_DOC},
169 {},
170 };
171
172 PyTypeObject Register_type = {
173 PyVarObject_HEAD_INIT(NULL, 0)
174 .tp_name = "_drgn.Register",
175 .tp_basicsize = sizeof(Register),
176 .tp_repr = (reprfunc)Register_repr,
177 // Doesn't reference any objects, no GC needed.
178 .tp_flags = Py_TPFLAGS_DEFAULT,
179 .tp_doc = drgn_Register_DOC,
180 .tp_richcompare = (richcmpfunc)Register_richcompare,
181 .tp_getset = Register_getset,
182 };