Debian Codegraph @

libdrgn/python/main.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include <elfutils/libdwfl.h>
5 #ifdef WITH_PCRE2
6 #define PCRE2_CODE_UNIT_WIDTH 8
7 #include <pcre2.h>
8 #endif
9
10 #include "drgnpy.h"
11 #include "../error.h"
12 #include "../path.h"
13
14 /* Basically PyModule_AddType(), which is only available since Python 3.9. */
15 static int add_type(PyObject *module, PyTypeObject *type)
16 {
17 int ret = PyType_Ready(type);
18 if (ret)
19 return ret;
20 const char *name = type->tp_name;
21 const char *dot = strrchr(type->tp_name, '.');
22 if (dot)
23 name = dot + 1;
24 return PyModule_AddObjectRef(module, name, (PyObject *)type);
25 }
26
27 static int add_bool(PyObject *module, const char *name, bool value)
28 {
29 return PyModule_AddObjectRef(module, name, value ? Py_True : Py_False);
30 }
31
32 PyObject *MissingDebugInfoError;
33 static PyObject *NoDefaultProgramError;
34 PyObject *ObjectAbsentError;
35 PyObject *OutOfBoundsError;
36
37 static _Thread_local Program *default_prog;
38
39 static PyObject *get_default_prog(PyObject *self, PyObject *_)
40 {
41 if (!default_prog) {
42 PyErr_SetString(NoDefaultProgramError, "no default program");
43 return NULL;
44 }
45 Py_INCREF(default_prog);
46 return (PyObject *)default_prog;
47 }
48
49 static PyObject *set_default_prog(PyObject *self, PyObject *arg)
50 {
51 if (arg == Py_None) {
52 Py_CLEAR(default_prog);
53 } else if (PyObject_TypeCheck(arg, &Program_type)) {
54 Py_INCREF(arg);
55 Py_XSETREF(default_prog, (Program *)arg);
56 } else {
57 PyErr_SetString(PyExc_TypeError,
58 "prog must be Program or None");
59 return NULL;
60 }
61 Py_RETURN_NONE;
62 }
63
64 static PyObject *filename_matches(PyObject *self, PyObject *args,
65 PyObject *kwds)
66 {
67 static char *keywords[] = {"haystack", "needle", NULL};
68 PATH_ARG(haystack_arg, .allow_none = true);
69 PATH_ARG(needle_arg, .allow_none = true);
70 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&:filename_matches",
71 keywords, path_converter,
72 &haystack_arg, path_converter,
73 &needle_arg))
74 return NULL;
75
76 struct path_iterator haystack = {
77 .components = (struct nstring [1]){},
78 .num_components = 0,
79 };
80 if (haystack_arg.path) {
81 haystack.components[0].str = haystack_arg.path;
82 haystack.components[0].len = haystack_arg.length;
83 haystack.num_components = 1;
84 }
85 struct path_iterator needle = {
86 .components = (struct nstring [1]){},
87 .num_components = 0,
88 };
89 if (needle_arg.path) {
90 needle.components[0].str = needle_arg.path;
91 needle.components[0].len = needle_arg.length;
92 needle.num_components = 1;
93 }
94 Py_RETURN_BOOL(path_ends_with(&haystack, &needle));
95 }
96
97 static PyObject *sizeof_(PyObject *self, PyObject *arg)
98 {
99 struct drgn_error *err;
100 uint64_t size;
101 if (PyObject_TypeCheck(arg, &DrgnType_type)) {
102 err = drgn_type_sizeof(((DrgnType *)arg)->type, &size);
103 } else if (PyObject_TypeCheck(arg, &DrgnObject_type)) {
104 err = drgn_object_sizeof(&((DrgnObject *)arg)->obj, &size);
105 } else if (PyUnicode_Check(arg)) {
106 if (!default_prog) {
107 PyErr_SetString(NoDefaultProgramError,
108 "no default program");
109 return NULL;
110 }
111
112 const char *name = PyUnicode_AsUTF8(arg);
113 if (!name)
114 return NULL;
115
116 drgn_in_python_guard();
117 struct drgn_qualified_type qualified_type;
118 err = drgn_program_find_type(&default_prog->prog, name, NULL,
119 &qualified_type);
120 if (!err) {
121 err = drgn_type_sizeof(qualified_type.type, &size);
122 } else if (drgn_error_catch(&err, DRGN_ERROR_LOOKUP)) {
123 DRGN_OBJECT(obj, &default_prog->prog);
124 err = drgn_program_find_object(&default_prog->prog,
125 name, NULL,
126 DRGN_FIND_OBJECT_ANY,
127 &obj);
128 if (!err)
129 err = drgn_object_sizeof(&obj, &size);
130 }
131 } else {
132 return PyErr_Format(PyExc_TypeError,
133 "expected Type, Object, or str, not %s",
134 Py_TYPE(arg)->tp_name);
135 }
136 if (err)
137 return set_drgn_error(err);
138 return PyLong_FromUInt64(size);
139 }
140
141 static bool
142 default_prog_find_type(PyObject *arg, struct drgn_qualified_type *ret)
143 {
144 struct drgn_error *err;
145
146 if (!default_prog) {
147 PyErr_SetString(NoDefaultProgramError, "no default program");
148 return false;
149 }
150
151 const char *name = PyUnicode_AsUTF8(arg);
152 if (!name)
153 return false;
154
155 bool clear = set_drgn_in_python();
156 err = drgn_program_find_type(&default_prog->prog, name, NULL, ret);
157 if (clear)
158 clear_drgn_in_python();
159 if (err) {
160 set_drgn_error(err);
161 return false;
162 }
163 return true;
164 }
165
166 static PyObject *alignof_(PyObject *self, PyObject *arg)
167 {
168 struct drgn_error *err;
169 struct drgn_qualified_type qualified_type;
170 if (PyObject_TypeCheck(arg, &DrgnType_type)) {
171 qualified_type.type = ((DrgnType *)arg)->type;
172 qualified_type.qualifiers = ((DrgnType *)arg)->qualifiers;
173 } else if (PyUnicode_Check(arg)) {
174 if (!default_prog_find_type(arg, &qualified_type))
175 return NULL;
176 } else {
177 return PyErr_Format(PyExc_TypeError,
178 "expected Type or str, not %s",
179 Py_TYPE(arg)->tp_name);
180 }
181 uint64_t size;
182 err = drgn_type_alignof(qualified_type, &size);
183 if (err)
184 return set_drgn_error(err);
185 return PyLong_FromUInt64(size);
186 }
187
188 static PyObject *offsetof_(PyObject *self, PyObject *args, PyObject *kwds)
189 {
190 struct drgn_error *err;
191
192 static char *keywords[] = {"type", "member", NULL};
193 PyObject *arg;
194 const char *member;
195 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Os:offsetof", keywords,
196 &arg, &member))
197 return NULL;
198
199 struct drgn_type *type;
200 if (PyObject_TypeCheck(arg, &DrgnType_type)) {
201 type = ((DrgnType *)arg)->type;
202 } else if (PyUnicode_Check(arg)) {
203 struct drgn_qualified_type qualified_type;
204 if (!default_prog_find_type(arg, &qualified_type))
205 return NULL;
206 type = qualified_type.type;
207 } else {
208 return PyErr_Format(PyExc_TypeError,
209 "expected Type or str, not %s",
210 Py_TYPE(arg)->tp_name);
211 }
212
213 uint64_t offset;
214 err = drgn_type_offsetof(type, member, &offset);
215 if (err)
216 return set_drgn_error(err);
217 return PyLong_FromUInt64(offset);
218 }
219
220 static PyMethodDef drgn_methods[] = {
221 {"get_default_prog", get_default_prog, METH_NOARGS,
222 drgn_get_default_prog_DOC},
223 {"set_default_prog", set_default_prog, METH_O,
224 drgn_set_default_prog_DOC},
225 {"filename_matches", (PyCFunction)filename_matches,
226 METH_VARARGS | METH_KEYWORDS, drgn_filename_matches_DOC},
227 {"NULL", (PyCFunction)DrgnObject_NULL, METH_VARARGS | METH_KEYWORDS,
228 drgn_NULL_DOC},
229 {"sizeof", (PyCFunction)sizeof_, METH_O, drgn_sizeof_DOC},
230 {"alignof", (PyCFunction)alignof_, METH_O, drgn_alignof_DOC},
231 {"offsetof", (PyCFunction)offsetof_, METH_VARARGS | METH_KEYWORDS,
232 drgn_offsetof_DOC},
233 {"cast", (PyCFunction)cast, METH_VARARGS | METH_KEYWORDS,
234 drgn_cast_DOC},
235 {"implicit_convert", (PyCFunction)implicit_convert,
236 METH_VARARGS | METH_KEYWORDS, drgn_implicit_convert_DOC},
237 {"reinterpret", (PyCFunction)reinterpret, METH_VARARGS | METH_KEYWORDS,
238 drgn_reinterpret_DOC},
239 {"container_of", (PyCFunction)DrgnObject_container_of,
240 METH_VARARGS | METH_KEYWORDS, drgn_container_of_DOC},
241 {"program_from_core_dump", (PyCFunction)program_from_core_dump,
242 METH_VARARGS | METH_KEYWORDS, drgn_program_from_core_dump_DOC},
243 {"program_from_kernel", (PyCFunction)program_from_kernel,
244 METH_NOARGS, drgn_program_from_kernel_DOC},
245 {"program_from_pid", (PyCFunction)program_from_pid,
246 METH_VARARGS | METH_KEYWORDS, drgn_program_from_pid_DOC},
247 {"_linux_helper_direct_mapping_offset",
248 (PyCFunction)drgnpy_linux_helper_direct_mapping_offset, METH_O},
249 {"_linux_helper_read_vm", (PyCFunction)drgnpy_linux_helper_read_vm,
250 METH_VARARGS | METH_KEYWORDS},
251 {"_linux_helper_follow_phys",
252 (PyCFunction)drgnpy_linux_helper_follow_phys,
253 METH_VARARGS | METH_KEYWORDS},
254 {"_linux_helper_per_cpu_ptr",
255 (PyCFunction)drgnpy_linux_helper_per_cpu_ptr,
256 METH_VARARGS | METH_KEYWORDS, drgn__linux_helper_per_cpu_ptr_DOC},
257 {"_linux_helper_cpu_curr", (PyCFunction)drgnpy_linux_helper_cpu_curr,
258 METH_VARARGS},
259 {"_linux_helper_idle_task", (PyCFunction)drgnpy_linux_helper_idle_task,
260 METH_VARARGS},
261 {"_linux_helper_task_thread_info",
262 (PyCFunction)drgnpy_linux_helper_task_thread_info,
263 METH_VARARGS | METH_KEYWORDS, drgn__linux_helper_task_thread_info_DOC},
264 {"_linux_helper_task_cpu", (PyCFunction)drgnpy_linux_helper_task_cpu,
265 METH_VARARGS | METH_KEYWORDS, drgn__linux_helper_task_cpu_DOC},
266 {"_linux_helper_task_on_cpu",
267 (PyCFunction)drgnpy_linux_helper_task_on_cpu,
268 METH_VARARGS | METH_KEYWORDS, drgn__linux_helper_task_on_cpu_DOC},
269 {"_linux_helper_xa_load",
270 (PyCFunction)drgnpy_linux_helper_xa_load,
271 METH_VARARGS | METH_KEYWORDS},
272 {"_linux_helper_idr_find", (PyCFunction)drgnpy_linux_helper_idr_find,
273 METH_VARARGS | METH_KEYWORDS},
274 {"_linux_helper_find_pid", (PyCFunction)drgnpy_linux_helper_find_pid,
275 METH_VARARGS},
276 {"_linux_helper_pid_task", (PyCFunction)drgnpy_linux_helper_pid_task,
277 METH_VARARGS | METH_KEYWORDS, drgn__linux_helper_pid_task_DOC},
278 {"_linux_helper_find_task", (PyCFunction)drgnpy_linux_helper_find_task,
279 METH_VARARGS},
280 {"_linux_helper_kaslr_offset", drgnpy_linux_helper_kaslr_offset,
281 METH_O},
282 {"_linux_helper_pgtable_l5_enabled",
283 drgnpy_linux_helper_pgtable_l5_enabled, METH_O},
284 {"_linux_helper_load_proc_kallsyms",
285 (PyCFunction)drgnpy_linux_helper_load_proc_kallsyms,
286 METH_VARARGS | METH_KEYWORDS},
287 {"_linux_helper_load_builtin_kallsyms",
288 (PyCFunction)drgnpy_linux_helper_load_builtin_kallsyms,
289 METH_VARARGS | METH_KEYWORDS},
290 {},
291 };
292
293 static struct PyModuleDef drgnmodule = {
294 PyModuleDef_HEAD_INIT,
295 "_drgn",
296 drgn_DOC,
297 -1,
298 drgn_methods,
299 };
300
301 // These are for type checking and aren't strictly required at runtime, but
302 // adding them anyways results in better pydoc output and saves us from fiddling
303 // with typing.TYPE_CHECKING/forward references.
304 static int add_type_aliases(PyObject *m)
305 {
306 _cleanup_pydecref_ PyObject *os_module = PyImport_ImportModule("os");
307 if (!os_module)
308 return -1;
309 _cleanup_pydecref_ PyObject *os_PathLike =
310 PyObject_GetAttrString(os_module, "PathLike");
311 if (!os_PathLike)
312 return -1;
313
314 _cleanup_pydecref_ PyObject *typing_module =
315 PyImport_ImportModule("typing");
316 if (!typing_module)
317 return -1;
318 _cleanup_pydecref_ PyObject *typing_Union =
319 PyObject_GetAttrString(typing_module, "Union");
320 if (!typing_Union)
321 return -1;
322
323 if (PyModule_Add(m, "IntegerLike",
324 PyObject_GetAttrString(typing_module, "SupportsIndex")))
325 return -1;
326
327 _cleanup_pydecref_ PyObject *item =
328 Py_BuildValue("OOO", &PyUnicode_Type, &PyBytes_Type,
329 os_PathLike);
330 if (!item)
331 return -1;
332 if (PyModule_Add(m, "Path", PyObject_GetItem(typing_Union, item)))
333 return -1;
334
335 return PyModule_Add(m, "MemorySearchIterator",
336 PyObject_GetAttrString(typing_module, "Iterator"));
337 }
338
339 PyMODINIT_FUNC PyInit__drgn(void); // Silence -Wmissing-prototypes.
340 DRGNPY_PUBLIC PyMODINIT_FUNC PyInit__drgn(void)
341 {
342 PyObject *m = PyModule_Create(&drgnmodule);
343 if (!m)
344 return NULL;
345
346 #define add_new_exception(m, name) ({ \
347 name = PyErr_NewExceptionWithDoc("_drgn." #name, \
348 drgn_##name##_DOC, NULL, \
349 NULL); \
350 !name || PyModule_AddObjectRef(m, #name, name); \
351 })
352
353 if (add_module_constants(m) ||
354 add_type(m, &DebugInfoOptions_type) ||
355 add_type(m, &Language_type) || add_languages() ||
356 add_type(m, &DrgnObject_type) ||
357 PyType_Ready(&MemorySearchIteratorWithBytes_type) ||
358 PyType_Ready(&MemorySearchIteratorWithInt_type) ||
359 PyType_Ready(&MemorySearchIteratorWithStr_type) ||
360 PyType_Ready(&MemorySearchIterator_type) ||
361 add_type(m, &Module_type) ||
362 add_type(m, &MainModule_type) ||
363 add_type(m, &SharedLibraryModule_type) ||
364 add_type(m, &VdsoModule_type) ||
365 add_type(m, &RelocatableModule_type) ||
366 add_type(m, &ExtraModule_type) ||
367 PyType_Ready(&ModuleIterator_type) ||
368 PyType_Ready(&ModuleIteratorWithNew_type) ||
369 add_WantedSupplementaryFile(m) ||
370 init_module_section_addresses() ||
371 PyType_Ready(&ModuleSectionAddressesIterator_type) ||
372 PyType_Ready(&ObjectIterator_type) ||
373 add_type(m, &Platform_type) ||
374 add_type(m, &Program_type) ||
375 add_type(m, &Register_type) ||
376 add_type(m, &SourceLocationList_type) ||
377 add_SourceLocation(m) ||
378 add_type(m, &StackFrame_type) ||
379 add_type(m, &StackTrace_type) ||
380 add_type(m, &Symbol_type) ||
381 add_type(m, &SymbolIndex_type) ||
382 add_type(m, &DrgnType_type) ||
383 add_type(m, &Thread_type) ||
384 add_type(m, &ThreadIterator_type) ||
385 add_type(m, &TypeEnumerator_type) ||
386 add_type(m, &TypeKindSet_type) ||
387 PyType_Ready(&TypeKindSetIterator_type) ||
388 init_type_kind_set() ||
389 add_type(m, &TypeMember_type) ||
390 add_type(m, &TypeParameter_type) ||
391 add_type(m, &TypeTemplateParameter_type) ||
392 add_new_exception(m, MissingDebugInfoError) ||
393 add_new_exception(m, NoDefaultProgramError) ||
394 add_new_exception(m, ObjectAbsentError) ||
395 add_new_exception(m, OutOfBoundsError) ||
396 add_type_aliases(m) ||
397 init_logging())
398 goto err;
399
400 FaultError_type.tp_base = (PyTypeObject *)PyExc_Exception;
401 if (add_type(m, &FaultError_type))
402 goto err;
403
404 ObjectNotFoundError_type.tp_base = (PyTypeObject *)PyExc_KeyError;
405 // KeyError.__str__() returns repr(args[0]). Use BaseException.__str__()
406 // instead, which returns str(args[0]).
407 ObjectNotFoundError_type.tp_str =
408 ((PyTypeObject *)PyExc_BaseException)->tp_str;
409 if (add_type(m, &ObjectNotFoundError_type))
410 goto err;
411
412 if (PyModule_Add(m, "host_platform",
413 Platform_wrap(&drgn_host_platform)))
414 goto err;
415
416 if (PyModule_AddStringConstant(m, "_elfutils_version",
417 dwfl_version(NULL)))
418 goto err;
419
420 if (add_bool(m, "_have_debuginfod", drgn_have_debuginfod()))
421 goto err;
422
423 if (add_bool(m, "_enable_dlopen_debuginfod",
424 #if ENABLE_DLOPEN_DEBUGINFOD
425 true
426 #else
427 false
428 #endif
429 ))
430 goto err;
431
432 if (add_bool(m, "_with_json_c",
433 #ifdef WITH_JSON_C
434 true
435 #else
436 false
437 #endif
438 ))
439 goto err;
440
441 if (add_bool(m, "_with_libkdumpfile",
442 #ifdef WITH_LIBKDUMPFILE
443 true
444 #else
445 false
446 #endif
447 ))
448 goto err;
449
450 if (add_bool(m, "_with_lzma",
451 #ifdef WITH_LZMA
452 true
453 #else
454 false
455 #endif
456 ))
457 goto err;
458
459 if (add_bool(m, "_with_pcre2",
460 #ifdef WITH_PCRE2
461 true
462 #else
463 false
464 #endif
465 ))
466 goto err;
467
468 // Allow tests to detect whether PCRE2 supports UTF-8 patterns within
469 // invalid UTF.
470 if (add_bool(m, "_with_pcre2_utf",
471 #if WITH_PCRE2 && (PCRE2_MAJOR > 10 || (PCRE2_MAJOR == 10 && PCRE2_MINOR >= 34))
472 true
473 #else
474 false
475 #endif
476 ))
477 goto err;
478
479 return m;
480
481 err:
482 Py_DECREF(m);
483 return NULL;
484 }
485
486 // On return from this function, three things need to be true:
487 //
488 // 1. The Python interpreter needs to be initialized.
489 // 2. The GIL needs to be held (and the caller needs to know whether to release
490 // it to restore the original state).
491 // 3. The _drgn module needs to be initialized.
492 //
493 // This can be called from many possible contexts (drgn CLI, standalone
494 // application using libdrgn, etc.), so we have to handle every possible initial
495 // state.
496 PyGILState_STATE drgn_initialize_python(bool *success_ret)
497 {
498 PyGILState_STATE gstate;
499 if (Py_IsInitialized()) {
500 gstate = PyGILState_Ensure();
501 } else {
502 gstate = PyGILState_UNLOCKED;
503 // If the Python interpreter wasn't already initialized, then we
504 // are in a standalone application using libdrgn. Set our
505 // imports up.
506 PyImport_AppendInittab("_drgn", PyInit__drgn);
507 Py_InitializeEx(0);
508 // Note: we don't have a good place to call Py_Finalize(), so we
509 // don't call it.
510 const char *env = getenv("PYTHONSAFEPATH");
511 if (!env || !env[0])
512 PyRun_SimpleString("import sys\nsys.path.insert(0, '')");
513 }
514
515 bool success = true;
516 if (!PyState_FindModule(&drgnmodule)) {
517 _cleanup_pydecref_ PyObject *m = PyImport_ImportModule("_drgn");
518 if (!m)
519 success = false;
520 }
521 *success_ret = success;
522 return gstate;
523 }