forked from osandov/drgn
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
355 lines (326 loc) · 10.7 KB
/
main.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <elfutils/libdwfl.h>
#ifdef WITH_KDUMPFILE
#include <libkdumpfile/kdumpfile.h>
#endif
#include "drgnpy.h"
#include "../path.h"
/* Basically PyModule_AddType(), which is only available since Python 3.9. */
static int add_type(PyObject *module, PyTypeObject *type)
{
int ret = PyType_Ready(type);
if (ret)
return ret;
const char *name = type->tp_name;
const char *dot = strrchr(type->tp_name, '.');
if (dot)
name = dot + 1;
Py_INCREF(type);
ret = PyModule_AddObject(module, name, (PyObject *)type);
if (ret)
Py_DECREF(type);
return ret;
}
PyObject *MissingDebugInfoError;
static PyObject *NoDefaultProgramError;
PyObject *ObjectAbsentError;
PyObject *OutOfBoundsError;
static _Thread_local PyObject *default_prog;
static PyObject *get_default_prog(PyObject *self, PyObject *_)
{
if (!default_prog) {
PyErr_SetString(NoDefaultProgramError, "no default program");
return NULL;
}
Py_INCREF(default_prog);
return default_prog;
}
static PyObject *set_default_prog(PyObject *self, PyObject *arg)
{
if (arg == Py_None) {
Py_CLEAR(default_prog);
} else if (PyObject_TypeCheck(arg, &Program_type)) {
Py_INCREF(arg);
Py_XSETREF(default_prog, arg);
} else {
PyErr_SetString(PyExc_TypeError,
"prog must be Program or None");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *filename_matches(PyObject *self, PyObject *args,
PyObject *kwds)
{
static char *keywords[] = {"haystack", "needle", NULL};
PATH_ARG(haystack_arg, .allow_none = true);
PATH_ARG(needle_arg, .allow_none = true);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&:filename_matches",
keywords, path_converter,
&haystack_arg, path_converter,
&needle_arg))
return NULL;
struct path_iterator haystack = {
.components = (struct nstring [1]){},
.num_components = 0,
};
if (haystack_arg.path) {
haystack.components[0].str = haystack_arg.path;
haystack.components[0].len = haystack_arg.length;
haystack.num_components = 1;
}
struct path_iterator needle = {
.components = (struct nstring [1]){},
.num_components = 0,
};
if (needle_arg.path) {
needle.components[0].str = needle_arg.path;
needle.components[0].len = needle_arg.length;
needle.num_components = 1;
}
Py_RETURN_BOOL(path_ends_with(&haystack, &needle));
}
static PyObject *sizeof_(PyObject *self, PyObject *arg)
{
struct drgn_error *err;
uint64_t size;
if (PyObject_TypeCheck(arg, &DrgnType_type)) {
err = drgn_type_sizeof(((DrgnType *)arg)->type, &size);
} else if (PyObject_TypeCheck(arg, &DrgnObject_type)) {
err = drgn_object_sizeof(&((DrgnObject *)arg)->obj, &size);
} else {
return PyErr_Format(PyExc_TypeError,
"expected Type or Object, not %s",
Py_TYPE(arg)->tp_name);
}
if (err)
return set_drgn_error(err);
return PyLong_FromUint64(size);
}
static PyObject *alignof_(PyObject *self, PyObject *arg)
{
struct drgn_error *err;
if (!PyObject_TypeCheck(arg, &DrgnType_type)) {
return PyErr_Format(PyExc_TypeError, "expected Type not %s",
Py_TYPE(arg)->tp_name);
}
struct drgn_qualified_type qualified_type = {
.type = ((DrgnType *)arg)->type,
.qualifiers = ((DrgnType *)arg)->qualifiers,
};
uint64_t size;
err = drgn_type_alignof(qualified_type, &size);
if (err)
return set_drgn_error(err);
return PyLong_FromUint64(size);
}
static PyObject *offsetof_(PyObject *self, PyObject *args, PyObject *kwds)
{
struct drgn_error *err;
static char *keywords[] = {"type", "member", NULL};
DrgnType *type;
const char *member;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!s:offsetof", keywords,
&DrgnType_type, &type, &member))
return NULL;
uint64_t offset;
err = drgn_type_offsetof(type->type, member, &offset);
if (err)
return set_drgn_error(err);
return PyLong_FromUint64(offset);
}
static PyMethodDef drgn_methods[] = {
{"get_default_prog", get_default_prog, METH_NOARGS,
drgn_get_default_prog_DOC},
{"set_default_prog", set_default_prog, METH_O,
drgn_set_default_prog_DOC},
{"filename_matches", (PyCFunction)filename_matches,
METH_VARARGS | METH_KEYWORDS, drgn_filename_matches_DOC},
{"NULL", (PyCFunction)DrgnObject_NULL, METH_VARARGS | METH_KEYWORDS,
drgn_NULL_DOC},
{"sizeof", (PyCFunction)sizeof_, METH_O, drgn_sizeof_DOC},
{"alignof", (PyCFunction)alignof_, METH_O, drgn_alignof_DOC},
{"offsetof", (PyCFunction)offsetof_, METH_VARARGS | METH_KEYWORDS,
drgn_offsetof_DOC},
{"cast", (PyCFunction)cast, METH_VARARGS | METH_KEYWORDS,
drgn_cast_DOC},
{"implicit_convert", (PyCFunction)implicit_convert,
METH_VARARGS | METH_KEYWORDS, drgn_implicit_convert_DOC},
{"reinterpret", (PyCFunction)reinterpret, METH_VARARGS | METH_KEYWORDS,
drgn_reinterpret_DOC},
{"container_of", (PyCFunction)DrgnObject_container_of,
METH_VARARGS | METH_KEYWORDS, drgn_container_of_DOC},
{"program_from_core_dump", (PyCFunction)program_from_core_dump,
METH_VARARGS | METH_KEYWORDS, drgn_program_from_core_dump_DOC},
{"program_from_kernel", (PyCFunction)program_from_kernel,
METH_NOARGS, drgn_program_from_kernel_DOC},
{"program_from_pid", (PyCFunction)program_from_pid,
METH_VARARGS | METH_KEYWORDS, drgn_program_from_pid_DOC},
{"_linux_helper_direct_mapping_offset",
(PyCFunction)drgnpy_linux_helper_direct_mapping_offset, METH_O},
{"_linux_helper_read_vm", (PyCFunction)drgnpy_linux_helper_read_vm,
METH_VARARGS | METH_KEYWORDS},
{"_linux_helper_follow_phys",
(PyCFunction)drgnpy_linux_helper_follow_phys,
METH_VARARGS | METH_KEYWORDS},
{"_linux_helper_per_cpu_ptr",
(PyCFunction)drgnpy_linux_helper_per_cpu_ptr,
METH_VARARGS | METH_KEYWORDS, drgn__linux_helper_per_cpu_ptr_DOC},
{"_linux_helper_cpu_curr", (PyCFunction)drgnpy_linux_helper_cpu_curr,
METH_VARARGS},
{"_linux_helper_idle_task", (PyCFunction)drgnpy_linux_helper_idle_task,
METH_VARARGS},
{"_linux_helper_task_thread_info",
(PyCFunction)drgnpy_linux_helper_task_thread_info,
METH_VARARGS | METH_KEYWORDS, drgn__linux_helper_task_thread_info_DOC},
{"_linux_helper_task_cpu", (PyCFunction)drgnpy_linux_helper_task_cpu,
METH_VARARGS | METH_KEYWORDS, drgn__linux_helper_task_cpu_DOC},
{"_linux_helper_xa_load",
(PyCFunction)drgnpy_linux_helper_xa_load,
METH_VARARGS | METH_KEYWORDS},
{"_linux_helper_idr_find", (PyCFunction)drgnpy_linux_helper_idr_find,
METH_VARARGS | METH_KEYWORDS},
{"_linux_helper_find_pid", (PyCFunction)drgnpy_linux_helper_find_pid,
METH_VARARGS},
{"_linux_helper_pid_task", (PyCFunction)drgnpy_linux_helper_pid_task,
METH_VARARGS | METH_KEYWORDS, drgn__linux_helper_pid_task_DOC},
{"_linux_helper_find_task", (PyCFunction)drgnpy_linux_helper_find_task,
METH_VARARGS},
{"_linux_helper_kaslr_offset", drgnpy_linux_helper_kaslr_offset,
METH_O},
{"_linux_helper_pgtable_l5_enabled",
drgnpy_linux_helper_pgtable_l5_enabled, METH_O},
{"_linux_helper_load_proc_kallsyms",
(PyCFunction)drgnpy_linux_helper_load_proc_kallsyms,
METH_VARARGS | METH_KEYWORDS},
{"_linux_helper_load_builtin_kallsyms",
(PyCFunction)drgnpy_linux_helper_load_builtin_kallsyms,
METH_VARARGS | METH_KEYWORDS},
{},
};
static struct PyModuleDef drgnmodule = {
PyModuleDef_HEAD_INIT,
"_drgn",
drgn_DOC,
-1,
drgn_methods,
};
// These are for type checking and aren't strictly required at runtime, but
// adding them anyways results in better pydoc output and saves us from fiddling
// with typing.TYPE_CHECKING/forward references.
static int add_type_aliases(PyObject *m)
{
_cleanup_pydecref_ PyObject *os_module = PyImport_ImportModule("os");
if (!os_module)
return -1;
_cleanup_pydecref_ PyObject *os_PathLike =
PyObject_GetAttrString(os_module, "PathLike");
if (!os_PathLike)
return -1;
_cleanup_pydecref_ PyObject *typing_module =
PyImport_ImportModule("typing");
if (!typing_module)
return -1;
_cleanup_pydecref_ PyObject *typing_Union =
PyObject_GetAttrString(typing_module, "Union");
if (!typing_Union)
return -1;
// This should be a subclass of typing.Protocol, but that is only
// available since Python 3.8.
PyObject *IntegerLike = PyType_FromSpec(&(PyType_Spec){
.name = "_drgn.IntegerLike",
.flags = Py_TPFLAGS_DEFAULT,
.slots = (PyType_Slot []){{0, NULL}},
});
if (!IntegerLike)
return -1;
if (PyModule_AddObject(m, "IntegerLike", IntegerLike) == -1) {
Py_DECREF(IntegerLike);
return -1;
}
_cleanup_pydecref_ PyObject *item =
Py_BuildValue("OOO", &PyUnicode_Type, &PyBytes_Type,
os_PathLike);
if (!item)
return -1;
PyObject *Path = PyObject_GetItem(typing_Union, item);
if (!Path)
return -1;
if (PyModule_AddObject(m, "Path", Path) == -1) {
Py_DECREF(Path);
return -1;
}
return 0;
}
PyMODINIT_FUNC PyInit__drgn(void); // Silence -Wmissing-prototypes.
DRGNPY_PUBLIC PyMODINIT_FUNC PyInit__drgn(void)
{
PyObject *m = PyModule_Create(&drgnmodule);
if (!m)
return NULL;
#define add_new_exception(m, name) ({ \
name = PyErr_NewExceptionWithDoc("_drgn." #name, \
drgn_##name##_DOC, NULL, \
NULL); \
if (name && PyModule_AddObject(m, #name, name)) \
Py_CLEAR(name); \
!name; \
})
if (add_module_constants(m) ||
add_type(m, &Language_type) || add_languages() ||
add_type(m, &DrgnObject_type) ||
PyType_Ready(&ObjectIterator_type) ||
add_type(m, &Platform_type) ||
add_type(m, &Program_type) ||
add_type(m, &Register_type) ||
add_type(m, &StackFrame_type) ||
add_type(m, &StackTrace_type) ||
add_type(m, &Symbol_type) ||
add_type(m, &SymbolIndex_type) ||
add_type(m, &DrgnType_type) ||
add_type(m, &Thread_type) ||
add_type(m, &ThreadIterator_type) ||
add_type(m, &TypeEnumerator_type) ||
add_type(m, &TypeKindSet_type) ||
PyType_Ready(&TypeKindSetIterator_type) ||
init_type_kind_set() ||
add_type(m, &TypeMember_type) ||
add_type(m, &TypeParameter_type) ||
add_type(m, &TypeTemplateParameter_type) ||
add_new_exception(m, MissingDebugInfoError) ||
add_new_exception(m, NoDefaultProgramError) ||
add_new_exception(m, ObjectAbsentError) ||
add_new_exception(m, OutOfBoundsError) ||
add_type_aliases(m) ||
init_logging())
goto err;
FaultError_type.tp_base = (PyTypeObject *)PyExc_Exception;
if (add_type(m, &FaultError_type))
goto err;
PyObject *host_platform_obj = Platform_wrap(&drgn_host_platform);
if (!host_platform_obj)
goto err;
if (PyModule_AddObject(m, "host_platform", host_platform_obj)) {
Py_DECREF(host_platform_obj);
goto err;
}
if (PyModule_AddStringConstant(m, "_elfutils_version",
dwfl_version(NULL)))
goto err;
PyObject *with_libkdumpfile;
#ifdef WITH_LIBKDUMPFILE
with_libkdumpfile = Py_True;
#else
with_libkdumpfile = Py_False;
#endif
Py_INCREF(with_libkdumpfile);
if (PyModule_AddObject(m, "_with_libkdumpfile", with_libkdumpfile)) {
Py_DECREF(with_libkdumpfile);
goto err;
}
return m;
err:
Py_DECREF(m);
return NULL;
}