Debian Codegraph @

libdrgn/python/debug_info_options.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 static PyObject *DebugInfoOptions_wrap_list(const char * const *list)
7 {
8 if (!list)
9 Py_RETURN_NONE;
10 size_t n = 0;
11 while (list[n])
12 n++;
13 _cleanup_pydecref_ PyObject *ret = PyTuple_New(n);
14 if (!ret)
15 return NULL;
16 for (size_t i = 0; i < n; i++) {
17 PyObject *item = PyUnicode_FromString(list[i]);
18 if (!item)
19 return NULL;
20 PyTuple_SET_ITEM(ret, i, item);
21 }
22 return_ptr(ret);
23 }
24
25 #define DebugInfoOptions_SETTER(name) \
26 static int DebugInfoOptions_set_##name(DebugInfoOptions *self, PyObject *value, \
27 void *arg) \
28 { \
29 SETTER_NO_DELETE(#name, value); \
30 if (!DebugInfoOptions_##name##_converter(value, self->options)) \
31 return -1; \
32 return 0; \
33 }
34
35 #define LIST_OPTION(name) \
36 static int DebugInfoOptions_##name##_converter(PyObject *o, void *p) \
37 { \
38 PATH_SEQUENCE_ARG(list, .null_terminate = true); \
39 if (!path_sequence_converter(o, &list)) \
40 return 0; \
41 struct drgn_error *err = \
42 drgn_debug_info_options_set_##name(p, list.paths); \
43 if (err) { \
44 set_drgn_error(err); \
45 return 0; \
46 } \
47 return 1; \
48 } \
49 \
50 static PyObject *DebugInfoOptions_get_##name(DebugInfoOptions *self, void *arg) \
51 { \
52 const char * const *list = \
53 drgn_debug_info_options_get_##name(self->options); \
54 return DebugInfoOptions_wrap_list(list); \
55 } \
56 DebugInfoOptions_SETTER(name)
57
58 #define BOOL_OPTION(name, default_value) \
59 static int DebugInfoOptions_##name##_converter(PyObject *o, void *p) \
60 { \
61 int r = PyObject_IsTrue(o); \
62 if (r < 0) \
63 return 0; \
64 drgn_debug_info_options_set_##name(p, r); \
65 return 1; \
66 } \
67 \
68 static PyObject *DebugInfoOptions_get_##name(DebugInfoOptions *self, void *arg) \
69 { \
70 Py_RETURN_BOOL(drgn_debug_info_options_get_##name(self->options)); \
71 } \
72 DebugInfoOptions_SETTER(name)
73
74 #define drgn_kmod_search_method_class KmodSearchMethod_class
75
76 #define ENUM_OPTION(name, type, default_value) \
77 static int DebugInfoOptions_##name##_converter(PyObject *o, void *p) \
78 { \
79 if (!PyObject_TypeCheck(o, (PyTypeObject *)type##_class)) { \
80 PyErr_Format(PyExc_TypeError, "%s must be %s", #name, \
81 ((PyTypeObject *)type##_class)->tp_name); \
82 return 0; \
83 } \
84 _cleanup_pydecref_ PyObject *value_obj = \
85 PyObject_GetAttrString(o, "value"); \
86 if (!value_obj) \
87 return 0; \
88 long value = PyLong_AsLong(value_obj); \
89 if (value == -1 && PyErr_Occurred()) \
90 return 0; \
91 drgn_debug_info_options_set_##name(p, value); \
92 return 1; \
93 } \
94 \
95 static PyObject *DebugInfoOptions_get_##name(DebugInfoOptions *self, void *arg) \
96 { \
97 return PyObject_CallFunction(type##_class, "i", \
98 drgn_debug_info_options_get_##name(self->options));\
99 } \
100 DebugInfoOptions_SETTER(name)
101
102 DRGN_DEBUG_INFO_OPTIONS
103
104 #undef ENUM_OPTION
105 #undef BOOL_OPTION
106 #undef LIST_OPTION
107
108 static inline void
109 drgn_debug_info_options_destroyp(struct drgn_debug_info_options **optionsp)
110 {
111 drgn_debug_info_options_destroy(*optionsp);
112 }
113
114 static DebugInfoOptions *DebugInfoOptions_new(PyTypeObject *subtype,
115 PyObject *args, PyObject *kwds)
116 {
117 struct drgn_error *err;
118
119 _cleanup_(drgn_debug_info_options_destroyp)
120 struct drgn_debug_info_options *options = NULL;
121 err = drgn_debug_info_options_create(&options);
122 if (err)
123 return set_drgn_error(err);
124
125 // Parse the positional options argument manually so that we can parse
126 // the keyword arguments directly into the struct
127 // drgn_debug_info_options.
128 if (PyTuple_GET_SIZE(args) > 0) {
129 PyObject *source = PyTuple_GET_ITEM(args, 0);
130 if (source != Py_None) {
131 if (!PyObject_TypeCheck(source,
132 &DebugInfoOptions_type)) {
133 PyErr_SetString(PyExc_TypeError,
134 "options must be DebugInfoOptions");
135 return NULL;
136 }
137 err = drgn_debug_info_options_copy(options,
138 ((DebugInfoOptions *)source)->options);
139 if (err) {
140 set_drgn_error(err);
141 return NULL;
142 }
143 }
144 }
145
146 #define BOOL_OPTION(name, default_value) LIST_OPTION(name)
147 #define ENUM_OPTION(name, type, default_value) LIST_OPTION(name)
148 static char *keywords[] = {
149 "",
150 #define LIST_OPTION(name) #name,
151 DRGN_DEBUG_INFO_OPTIONS
152 #undef LIST_OPTION
153 NULL,
154 };
155 PyObject *unused;
156 if (!PyArg_ParseTupleAndKeywords(args, kwds,
157 "|O$"
158 #define LIST_OPTION(name) "O&"
159 DRGN_DEBUG_INFO_OPTIONS
160 #undef LIST_OPTION
161 ":DebugInfoOptions", keywords, &unused
162 #define LIST_OPTION(name) , DebugInfoOptions_##name##_converter, options
163 DRGN_DEBUG_INFO_OPTIONS
164 #undef ENUM_OPTION
165 #undef BOOL_OPTION
166 #undef LIST_OPTION
167 ))
168 return NULL;
169
170 DebugInfoOptions *ret =
171 (DebugInfoOptions *)subtype->tp_alloc(subtype, 0);
172 if (ret)
173 ret->options = no_cleanup_ptr(options);
174 return ret;
175 }
176
177 static void DebugInfoOptions_dealloc(DebugInfoOptions *self)
178 {
179 PyObject_GC_UnTrack(self);
180 if (self->prog)
181 Py_DECREF(self->prog);
182 else
183 drgn_debug_info_options_destroy(self->options);
184 Py_TYPE(self)->tp_free((PyObject *)self);
185 }
186
187 static int DebugInfoOptions_traverse(DebugInfoOptions *self, visitproc visit,
188 void *arg)
189 {
190 Py_VISIT(self->prog);
191 return 0;
192 }
193
194 static PyGetSetDef DebugInfoOptions_getset[] = {
195 #define LIST_OPTION(name) \
196 {#name, (getter)DebugInfoOptions_get_##name, \
197 (setter)DebugInfoOptions_set_##name, \
198 drgn_DebugInfoOptions_##name##_DOC},
199 #define BOOL_OPTION(name, default_value) LIST_OPTION(name)
200 #define ENUM_OPTION(name, type, default_value) LIST_OPTION(name)
201 DRGN_DEBUG_INFO_OPTIONS
202 #undef ENUM_OPTION
203 #undef BOOL_OPTION
204 #undef LIST_OPTION
205 {},
206 };
207
208 static PyObject *DebugInfoOptions_repr(PyObject *self)
209 {
210 _cleanup_pydecref_ PyObject *parts = PyList_New(0);
211 if (!parts)
212 return NULL;
213 if (append_string(parts, "DebugInfoOptions("))
214 return NULL;
215 bool first = true;
216 for (size_t i = 0; DebugInfoOptions_getset[i].name; i++) {
217 if (append_format(parts, "%s%s=", first ? "" : ", ",
218 DebugInfoOptions_getset[i].name)
219 || append_attr_repr(parts, self,
220 DebugInfoOptions_getset[i].name))
221 return NULL;
222 first = false;
223 }
224 if (append_string(parts, ")"))
225 return NULL;
226 return join_strings(parts);
227 }
228
229 PyTypeObject DebugInfoOptions_type = {
230 PyVarObject_HEAD_INIT(NULL, 0)
231 .tp_name = "_drgn.DebugInfoOptions",
232 .tp_dealloc = (destructor)DebugInfoOptions_dealloc,
233 .tp_basicsize = sizeof(DebugInfoOptions),
234 .tp_repr = DebugInfoOptions_repr,
235 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
236 .tp_doc = drgn_DebugInfoOptions_DOC,
237 .tp_traverse = (traverseproc)DebugInfoOptions_traverse,
238 .tp_getset = DebugInfoOptions_getset,
239 .tp_new = (newfunc)DebugInfoOptions_new,
240 };