| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include "drgnpy.h" |
| 5 |
#include "../util.h" |
| 6 |
|
| 7 |
static PyObject *WantedSupplementaryFile_class; |
| 8 |
|
| 9 |
int add_WantedSupplementaryFile(PyObject *m) |
| 10 |
{ |
| 11 |
_cleanup_pydecref_ PyObject *collections = |
| 12 |
PyImport_ImportModule("collections"); |
| 13 |
_cleanup_pydecref_ PyObject *namedtuple = |
| 14 |
PyObject_GetAttrString(collections, "namedtuple"); |
| 15 |
if (!namedtuple) |
| 16 |
return -1; |
| 17 |
WantedSupplementaryFile_class = |
| 18 |
PyObject_CallFunction(namedtuple, "s[ssss]", |
| 19 |
"WantedSupplementaryFile", "kind", "path", |
| 20 |
"supplementary_path", "checksum"); |
| 21 |
return PyModule_AddObjectRef(m, "WantedSupplementaryFile", |
| 22 |
WantedSupplementaryFile_class); |
| 23 |
} |
| 24 |
|
| 25 |
PyObject *Module_wrap(struct drgn_module *module) |
| 26 |
{ |
| 27 |
PyTypeObject *type; |
| 28 |
SWITCH_ENUM(drgn_module_kind(module)) { |
| 29 |
case DRGN_MODULE_MAIN: |
| 30 |
type = &MainModule_type; |
| 31 |
break; |
| 32 |
case DRGN_MODULE_SHARED_LIBRARY: |
| 33 |
type = &SharedLibraryModule_type; |
| 34 |
break; |
| 35 |
case DRGN_MODULE_VDSO: |
| 36 |
type = &VdsoModule_type; |
| 37 |
break; |
| 38 |
case DRGN_MODULE_RELOCATABLE: |
| 39 |
type = &RelocatableModule_type; |
| 40 |
break; |
| 41 |
case DRGN_MODULE_EXTRA: |
| 42 |
type = &ExtraModule_type; |
| 43 |
break; |
| 44 |
default: |
| 45 |
UNREACHABLE(); |
| 46 |
} |
| 47 |
Module *ret = (Module *)type->tp_alloc(type, 0); |
| 48 |
if (ret) { |
| 49 |
struct drgn_program *prog = drgn_module_program(module); |
| 50 |
Py_INCREF(container_of(prog, Program, prog)); |
| 51 |
ret->module = module; |
| 52 |
} |
| 53 |
return (PyObject *)ret; |
| 54 |
} |
| 55 |
|
| 56 |
static void Module_dealloc(Module *self) |
| 57 |
{ |
| 58 |
PyObject_GC_UnTrack(self); |
| 59 |
if (self->module) |
| 60 |
Py_DECREF(Module_prog(self)); |
| 61 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 62 |
} |
| 63 |
|
| 64 |
static int Module_traverse(Module *self, visitproc visit, void *arg) |
| 65 |
{ |
| 66 |
if (self->module) |
| 67 |
Py_VISIT(Module_prog(self)); |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
static int append_module_repr_common(PyObject *parts, Module *self, |
| 72 |
const char *method_name) |
| 73 |
{ |
| 74 |
if (append_format(parts, "prog.%s_module(name=", method_name) < 0 || |
| 75 |
append_attr_repr(parts, (PyObject *)self, "name") < 0) |
| 76 |
return -1; |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static PyObject *Module_repr(Module *self) |
| 81 |
{ |
| 82 |
_cleanup_pydecref_ PyObject *parts = PyList_New(0); |
| 83 |
if (!parts) |
| 84 |
return NULL; |
| 85 |
|
| 86 |
SWITCH_ENUM(drgn_module_kind(self->module)) { |
| 87 |
case DRGN_MODULE_MAIN: |
| 88 |
if (append_module_repr_common(parts, self, "main") < 0) |
| 89 |
return NULL; |
| 90 |
break; |
| 91 |
case DRGN_MODULE_SHARED_LIBRARY: |
| 92 |
if (append_module_repr_common(parts, self, |
| 93 |
"shared_library") |
| 94 |
|| append_string(parts, ", dynamic_address=") |
| 95 |
|| append_u64_hex(parts, drgn_module_info(self->module))) |
| 96 |
return NULL; |
| 97 |
break; |
| 98 |
case DRGN_MODULE_VDSO: |
| 99 |
if (append_module_repr_common(parts, self, "vdso") |
| 100 |
|| append_string(parts, ", dynamic_address=") |
| 101 |
|| append_u64_hex(parts, drgn_module_info(self->module))) |
| 102 |
return NULL; |
| 103 |
break; |
| 104 |
case DRGN_MODULE_RELOCATABLE: |
| 105 |
if (append_module_repr_common(parts, self, "relocatable") |
| 106 |
|| append_string(parts, ", address=") |
| 107 |
|| append_u64_hex(parts, drgn_module_info(self->module))) |
| 108 |
return NULL; |
| 109 |
break; |
| 110 |
case DRGN_MODULE_EXTRA: |
| 111 |
if (append_module_repr_common(parts, self, "extra") |
| 112 |
|| append_string(parts, ", id=") |
| 113 |
|| append_u64_hex(parts, drgn_module_info(self->module))) |
| 114 |
return NULL; |
| 115 |
break; |
| 116 |
default: |
| 117 |
UNREACHABLE(); |
| 118 |
} |
| 119 |
if (append_string(parts, ")")) |
| 120 |
return NULL; |
| 121 |
return join_strings(parts); |
| 122 |
} |
| 123 |
|
| 124 |
static PyObject *Module_richcompare(Module *self, PyObject *other, int op) |
| 125 |
{ |
| 126 |
if ((op != Py_EQ && op != Py_NE) || |
| 127 |
!PyObject_TypeCheck(other, &Module_type)) |
| 128 |
Py_RETURN_NOTIMPLEMENTED; |
| 129 |
int ret = self->module == ((Module *)other)->module; |
| 130 |
if (op == Py_NE) |
| 131 |
ret = !ret; |
| 132 |
Py_RETURN_BOOL(ret); |
| 133 |
} |
| 134 |
|
| 135 |
static Py_hash_t Module_hash(Module *self) |
| 136 |
{ |
| 137 |
return Py_HashPointer(self->module); |
| 138 |
} |
| 139 |
|
| 140 |
static PyObject *Module_wanted_supplementary_debug_file(Module *self) |
| 141 |
{ |
| 142 |
const char *debug_file_path, *supplementary_path; |
| 143 |
const void *checksum; |
| 144 |
size_t checksum_len; |
| 145 |
enum drgn_supplementary_file_kind kind = |
| 146 |
drgn_module_wanted_supplementary_debug_file(self->module, |
| 147 |
&debug_file_path, |
| 148 |
&supplementary_path, |
| 149 |
&checksum, |
| 150 |
&checksum_len); |
| 151 |
if (kind == DRGN_SUPPLEMENTARY_FILE_NONE) { |
| 152 |
return PyErr_Format(PyExc_ValueError, |
| 153 |
"module does not want supplementary debug file"); |
| 154 |
} |
| 155 |
return PyObject_CallFunction(WantedSupplementaryFile_class, |
| 156 |
"NO&O&y#", |
| 157 |
PyObject_CallFunction(SupplementaryFileKind_class, |
| 158 |
"k", |
| 159 |
(unsigned long)kind), |
| 160 |
PyUnicode_DecodeFSDefault, debug_file_path, |
| 161 |
PyUnicode_DecodeFSDefault, |
| 162 |
supplementary_path, checksum, |
| 163 |
(Py_ssize_t)checksum_len); |
| 164 |
} |
| 165 |
|
| 166 |
static PyObject *Module_try_file(Module *self, PyObject *args, PyObject *kwds) |
| 167 |
{ |
| 168 |
struct drgn_error *err; |
| 169 |
static char *keywords[] = { "path", "fd", "force", NULL }; |
| 170 |
struct path_arg path = {}; |
| 171 |
int fd = -1; |
| 172 |
int force = 0; |
| 173 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|$ip:try_file", keywords, |
| 174 |
path_converter, &path, &fd, &force)) |
| 175 |
return NULL; |
| 176 |
err = drgn_module_try_file(self->module, path.path, fd, force); |
| 177 |
path_cleanup(&path); |
| 178 |
if (err) |
| 179 |
return set_drgn_error(err); |
| 180 |
Py_RETURN_NONE; |
| 181 |
} |
| 182 |
|
| 183 |
static Program *Module_get_prog(Module *self, void *arg) |
| 184 |
{ |
| 185 |
Program *prog = Module_prog(self); |
| 186 |
Py_INCREF(prog); |
| 187 |
return prog; |
| 188 |
} |
| 189 |
|
| 190 |
static PyObject *Module_get_name(Module *self, void *arg) |
| 191 |
{ |
| 192 |
return PyUnicode_DecodeFSDefault(drgn_module_name(self->module)); |
| 193 |
} |
| 194 |
|
| 195 |
static PyObject *Module_get_address_ranges(Module *self, void *arg) |
| 196 |
{ |
| 197 |
size_t n; |
| 198 |
if (!drgn_module_num_address_ranges(self->module, &n)) |
| 199 |
Py_RETURN_NONE; |
| 200 |
_cleanup_pydecref_ PyObject *ret = PyTuple_New(n); |
| 201 |
if (!ret) |
| 202 |
return NULL; |
| 203 |
for (size_t i = 0; i < n; i++) { |
| 204 |
uint64_t start, end; |
| 205 |
drgn_module_address_range(self->module, i, &start, &end); |
| 206 |
PyObject *range = Py_BuildValue("KK", (unsigned long long)start, |
| 207 |
(unsigned long long)end); |
| 208 |
if (!range) |
| 209 |
return NULL; |
| 210 |
PyTuple_SET_ITEM(ret, i, range); |
| 211 |
} |
| 212 |
return_ptr(ret); |
| 213 |
} |
| 214 |
|
| 215 |
DEFINE_VECTOR(uint64_pair_vector, uint64_t [2]); |
| 216 |
|
| 217 |
static int Module_set_address_ranges(Module *self, PyObject *value, void *arg) |
| 218 |
{ |
| 219 |
SETTER_NO_DELETE("address_ranges", value); |
| 220 |
|
| 221 |
if (value == Py_None) { |
| 222 |
drgn_module_unset_address_ranges(self->module); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
struct drgn_error *err; |
| 227 |
_cleanup_pydecref_ PyObject *it = PyObject_GetIter(value); |
| 228 |
if (!it) |
| 229 |
return -1; |
| 230 |
|
| 231 |
Py_ssize_t length_hint = PyObject_LengthHint(value, 1); |
| 232 |
if (length_hint == -1) |
| 233 |
return -1; |
| 234 |
|
| 235 |
VECTOR(uint64_pair_vector, ranges); |
| 236 |
if (!uint64_pair_vector_reserve(&ranges, length_hint)) { |
| 237 |
PyErr_NoMemory(); |
| 238 |
return -1; |
| 239 |
} |
| 240 |
|
| 241 |
for (;;) { |
| 242 |
_cleanup_pydecref_ PyObject *item = PyIter_Next(it); |
| 243 |
if (!item) |
| 244 |
break; |
| 245 |
|
| 246 |
if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 247 |
PyErr_SetString(PyExc_TypeError, |
| 248 |
"address_ranges must None or sequence of (int, int)"); |
| 249 |
return -1; |
| 250 |
} |
| 251 |
uint64_t range[2]; |
| 252 |
if (PyLong_AsUInt64(PyTuple_GET_ITEM(item, 0), &range[0]) |
| 253 |
|| PyLong_AsUInt64(PyTuple_GET_ITEM(item, 1), &range[1])) |
| 254 |
return -1; |
| 255 |
|
| 256 |
if (!uint64_pair_vector_append(&ranges, &range)) { |
| 257 |
PyErr_NoMemory(); |
| 258 |
return -1; |
| 259 |
} |
| 260 |
} |
| 261 |
if (PyErr_Occurred()) |
| 262 |
return -1; |
| 263 |
|
| 264 |
err = drgn_module_set_address_ranges(self->module, |
| 265 |
uint64_pair_vector_begin(&ranges), |
| 266 |
uint64_pair_vector_size(&ranges)); |
| 267 |
if (err) { |
| 268 |
set_drgn_error(err); |
| 269 |
return -1; |
| 270 |
} |
| 271 |
return 0; |
| 272 |
} |
| 273 |
|
| 274 |
static PyObject *Module_get_address_range(Module *self, void *arg) |
| 275 |
{ |
| 276 |
size_t n; |
| 277 |
if (!drgn_module_num_address_ranges(self->module, &n)) |
| 278 |
Py_RETURN_NONE; |
| 279 |
if (n == 0) { |
| 280 |
return Py_BuildValue("ii", 0, 0); |
| 281 |
} else if (n == 1) { |
| 282 |
uint64_t start, end; |
| 283 |
drgn_module_address_range(self->module, 0, &start, &end); |
| 284 |
return Py_BuildValue("KK", (unsigned long long)start, |
| 285 |
(unsigned long long)end); |
| 286 |
} else { |
| 287 |
PyErr_SetString(PyExc_ValueError, |
| 288 |
"module has multiple address ranges"); |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
static int Module_set_address_range(Module *self, PyObject *value, void *arg) |
| 294 |
{ |
| 295 |
SETTER_NO_DELETE("address_range", value); |
| 296 |
|
| 297 |
if (value == Py_None) { |
| 298 |
drgn_module_unset_address_ranges(self->module); |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
struct drgn_error *err; |
| 303 |
if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) { |
| 304 |
PyErr_SetString(PyExc_TypeError, |
| 305 |
"address_range must be None or (int, int)"); |
| 306 |
return -1; |
| 307 |
} |
| 308 |
uint64_t start, end; |
| 309 |
if (PyLong_AsUInt64(PyTuple_GET_ITEM(value, 0), &start) |
| 310 |
|| PyLong_AsUInt64(PyTuple_GET_ITEM(value, 1), &end)) |
| 311 |
return -1; |
| 312 |
|
| 313 |
if (start == 0 && end == 0) { |
| 314 |
err = drgn_module_set_address_ranges(self->module, NULL, 0); |
| 315 |
} else { |
| 316 |
err = drgn_module_set_address_range(self->module, start, end); |
| 317 |
} |
| 318 |
if (err) { |
| 319 |
set_drgn_error(err); |
| 320 |
return -1; |
| 321 |
} |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
static PyObject *Module_get_build_id(Module *self, void *arg) |
| 326 |
{ |
| 327 |
const void *build_id; |
| 328 |
size_t build_id_len; |
| 329 |
if (!drgn_module_build_id(self->module, &build_id, &build_id_len)) |
| 330 |
Py_RETURN_NONE; |
| 331 |
return PyBytes_FromStringAndSize(build_id, build_id_len); |
| 332 |
} |
| 333 |
|
| 334 |
static int Module_set_build_id(Module *self, PyObject *value, void *arg) |
| 335 |
{ |
| 336 |
SETTER_NO_DELETE("build_id", value); |
| 337 |
struct drgn_error *err; |
| 338 |
if (value == Py_None) { |
| 339 |
err = drgn_module_set_build_id(self->module, NULL, 0); |
| 340 |
} else { |
| 341 |
Py_buffer buffer; |
| 342 |
int ret = PyObject_GetBuffer(value, &buffer, PyBUF_SIMPLE); |
| 343 |
if (ret) |
| 344 |
return ret; |
| 345 |
|
| 346 |
if (buffer.len == 0) { |
| 347 |
PyErr_SetString(PyExc_ValueError, |
| 348 |
"build ID cannot be empty"); |
| 349 |
PyBuffer_Release(&buffer); |
| 350 |
return -1; |
| 351 |
} |
| 352 |
|
| 353 |
err = drgn_module_set_build_id(self->module, buffer.buf, |
| 354 |
buffer.len); |
| 355 |
PyBuffer_Release(&buffer); |
| 356 |
} |
| 357 |
if (err) { |
| 358 |
set_drgn_error(err); |
| 359 |
return -1; |
| 360 |
} |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
static DrgnObject *Module_get_object(Module *self, void *arg) |
| 365 |
{ |
| 366 |
|
| 367 |
Program *prog_obj = Module_prog(self); |
| 368 |
_cleanup_pydecref_ DrgnObject *ret = DrgnObject_alloc(prog_obj); |
| 369 |
if (!ret) |
| 370 |
return NULL; |
| 371 |
|
| 372 |
struct drgn_error *err = drgn_module_object(self->module, &ret->obj); |
| 373 |
if (err) |
| 374 |
return set_drgn_error(err); |
| 375 |
return_ptr(ret); |
| 376 |
} |
| 377 |
|
| 378 |
static int Module_set_object(Module *self, PyObject *value, void *arg) |
| 379 |
{ |
| 380 |
SETTER_NO_DELETE("object", value); |
| 381 |
if (!PyObject_TypeCheck(value, &DrgnObject_type)) { |
| 382 |
PyErr_SetString(PyExc_TypeError, "object must be a drgn.Object"); |
| 383 |
return -1; |
| 384 |
} |
| 385 |
DrgnObject *object = (DrgnObject *)value; |
| 386 |
|
| 387 |
struct drgn_error *err = drgn_module_set_object(self->module, &object->obj); |
| 388 |
if (err) |
| 389 |
set_drgn_error(err); |
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
#define MODULE_FILE_STATUS_GETSET(which) \ |
| 394 |
static PyObject *Module_wants_##which##_file(Module *self) \ |
| 395 |
{ \ |
| 396 |
Py_RETURN_BOOL(drgn_module_wants_##which##_file(self->module)); \ |
| 397 |
} \ |
| 398 |
\ |
| 399 |
static PyObject *Module_get_##which##_file_status(Module *self, void *arg) \ |
| 400 |
{ \ |
| 401 |
return PyObject_CallFunction(ModuleFileStatus_class, "i", \ |
| 402 |
(int)drgn_module_##which##_file_status(self->module));\ |
| 403 |
} \ |
| 404 |
\ |
| 405 |
static int Module_set_##which##_file_status(Module *self, PyObject *value, \ |
| 406 |
void *arg) \ |
| 407 |
{ \ |
| 408 |
SETTER_NO_DELETE(#which, value); \ |
| 409 |
if (!PyObject_TypeCheck(value, \ |
| 410 |
(PyTypeObject *)ModuleFileStatus_class)) { \ |
| 411 |
PyErr_SetString(PyExc_TypeError, \ |
| 412 |
#which "_file_status must be ModuleFileStatus");\ |
| 413 |
return -1; \ |
| 414 |
} \ |
| 415 |
_cleanup_pydecref_ PyObject *value_obj = \ |
| 416 |
PyObject_GetAttrString(value, "value"); \ |
| 417 |
if (!value_obj) \ |
| 418 |
return -1; \ |
| 419 |
long status = PyLong_AsLong(value_obj); \ |
| 420 |
if (status == -1 && PyErr_Occurred()) \ |
| 421 |
return -1; \ |
| 422 |
\ |
| 423 |
if (drgn_module_set_##which##_file_status(self->module, status)) \ |
| 424 |
return 0; \ |
| 425 |
\ |
| 426 |
_cleanup_pydecref_ PyObject *old_status = \ |
| 427 |
Module_get_##which##_file_status(self, NULL); \ |
| 428 |
if (!old_status) \ |
| 429 |
return -1; \ |
| 430 |
PyErr_Format(PyExc_ValueError, \ |
| 431 |
"cannot change " #which "_file_status from %S to %S", \ |
| 432 |
old_status, value); \ |
| 433 |
return -1; \ |
| 434 |
} |
| 435 |
MODULE_FILE_STATUS_GETSET(loaded) |
| 436 |
MODULE_FILE_STATUS_GETSET(debug) |
| 437 |
|
| 438 |
static PyObject *Module_get_loaded_file_path(Module *self, void *arg) |
| 439 |
{ |
| 440 |
const char *path = drgn_module_loaded_file_path(self->module); |
| 441 |
if (!path) |
| 442 |
Py_RETURN_NONE; |
| 443 |
return PyUnicode_DecodeFSDefault(path); |
| 444 |
} |
| 445 |
|
| 446 |
static PyObject *Module_get_loaded_file_bias(Module *self, void *arg) |
| 447 |
{ |
| 448 |
if (!drgn_module_loaded_file_path(self->module)) |
| 449 |
Py_RETURN_NONE; |
| 450 |
return PyLong_FromUInt64(drgn_module_loaded_file_bias(self->module)); |
| 451 |
} |
| 452 |
|
| 453 |
static PyObject *Module_get_debug_file_path(Module *self, void *arg) |
| 454 |
{ |
| 455 |
const char *path = drgn_module_debug_file_path(self->module); |
| 456 |
if (!path) |
| 457 |
Py_RETURN_NONE; |
| 458 |
return PyUnicode_DecodeFSDefault(path); |
| 459 |
} |
| 460 |
|
| 461 |
static PyObject *Module_get_debug_file_bias(Module *self, void *arg) |
| 462 |
{ |
| 463 |
if (!drgn_module_debug_file_path(self->module)) |
| 464 |
Py_RETURN_NONE; |
| 465 |
return PyLong_FromUInt64(drgn_module_debug_file_bias(self->module)); |
| 466 |
} |
| 467 |
|
| 468 |
static PyObject *Module_get_supplementary_debug_file_kind(Module *self, |
| 469 |
void *arg) |
| 470 |
{ |
| 471 |
enum drgn_supplementary_file_kind kind = |
| 472 |
drgn_module_supplementary_debug_file_kind(self->module); |
| 473 |
if (kind == DRGN_SUPPLEMENTARY_FILE_NONE) |
| 474 |
Py_RETURN_NONE; |
| 475 |
return PyObject_CallFunction(SupplementaryFileKind_class, "k", |
| 476 |
(unsigned long)kind); |
| 477 |
} |
| 478 |
|
| 479 |
static PyObject *Module_get_supplementary_debug_file_path(Module *self, |
| 480 |
void *arg) |
| 481 |
{ |
| 482 |
const char *path = |
| 483 |
drgn_module_supplementary_debug_file_path(self->module); |
| 484 |
if (!path) |
| 485 |
Py_RETURN_NONE; |
| 486 |
return PyUnicode_DecodeFSDefault(path); |
| 487 |
} |
| 488 |
|
| 489 |
static PyMethodDef Module_methods[] = { |
| 490 |
{"wants_loaded_file", (PyCFunction)Module_wants_loaded_file, |
| 491 |
METH_NOARGS, drgn_Module_wants_loaded_file_DOC}, |
| 492 |
{"wants_debug_file", (PyCFunction)Module_wants_debug_file, METH_NOARGS, |
| 493 |
drgn_Module_wants_debug_file_DOC}, |
| 494 |
{"wanted_supplementary_debug_file", |
| 495 |
(PyCFunction)Module_wanted_supplementary_debug_file, METH_NOARGS, |
| 496 |
drgn_Module_wanted_supplementary_debug_file_DOC}, |
| 497 |
{"try_file", (PyCFunction)Module_try_file, |
| 498 |
METH_VARARGS | METH_KEYWORDS, drgn_Module_try_file_DOC}, |
| 499 |
{}, |
| 500 |
}; |
| 501 |
|
| 502 |
static PyGetSetDef Module_getset[] = { |
| 503 |
{"prog", (getter)Module_get_prog, NULL, drgn_Module_prog_DOC}, |
| 504 |
{"name", (getter)Module_get_name, NULL, drgn_Module_name_DOC}, |
| 505 |
{"address_ranges", (getter)Module_get_address_ranges, |
| 506 |
(setter)Module_set_address_ranges, drgn_Module_address_ranges_DOC}, |
| 507 |
{"address_range", (getter)Module_get_address_range, |
| 508 |
(setter)Module_set_address_range, drgn_Module_address_range_DOC}, |
| 509 |
{"build_id", (getter)Module_get_build_id, (setter)Module_set_build_id, |
| 510 |
drgn_Module_build_id_DOC}, |
| 511 |
{"object", (getter)Module_get_object, (setter)Module_set_object, |
| 512 |
drgn_Module_object_DOC}, |
| 513 |
{"loaded_file_status", (getter)Module_get_loaded_file_status, |
| 514 |
(setter)Module_set_loaded_file_status, |
| 515 |
drgn_Module_loaded_file_status_DOC}, |
| 516 |
{"loaded_file_path", (getter)Module_get_loaded_file_path, NULL, |
| 517 |
drgn_Module_loaded_file_path_DOC}, |
| 518 |
{"loaded_file_bias", (getter)Module_get_loaded_file_bias, NULL, |
| 519 |
drgn_Module_loaded_file_bias_DOC}, |
| 520 |
{"debug_file_status", (getter)Module_get_debug_file_status, |
| 521 |
(setter)Module_set_debug_file_status, |
| 522 |
drgn_Module_debug_file_status_DOC}, |
| 523 |
{"debug_file_path", (getter)Module_get_debug_file_path, NULL, |
| 524 |
drgn_Module_debug_file_path_DOC}, |
| 525 |
{"debug_file_bias", (getter)Module_get_debug_file_bias, NULL, |
| 526 |
drgn_Module_debug_file_bias_DOC}, |
| 527 |
{"supplementary_debug_file_kind", |
| 528 |
(getter)Module_get_supplementary_debug_file_kind, NULL, |
| 529 |
drgn_Module_supplementary_debug_file_kind_DOC}, |
| 530 |
{"supplementary_debug_file_path", |
| 531 |
(getter)Module_get_supplementary_debug_file_path, NULL, |
| 532 |
drgn_Module_supplementary_debug_file_path_DOC}, |
| 533 |
{}, |
| 534 |
}; |
| 535 |
|
| 536 |
PyTypeObject Module_type = { |
| 537 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 538 |
.tp_name = "_drgn.Module", |
| 539 |
.tp_basicsize = sizeof(Module), |
| 540 |
.tp_dealloc = (destructor)Module_dealloc, |
| 541 |
.tp_repr = (reprfunc)Module_repr, |
| 542 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, |
| 543 |
.tp_doc = drgn_Module_DOC, |
| 544 |
.tp_traverse = (traverseproc)Module_traverse, |
| 545 |
.tp_richcompare = (richcmpfunc)Module_richcompare, |
| 546 |
.tp_hash = (hashfunc)Module_hash, |
| 547 |
.tp_methods = Module_methods, |
| 548 |
.tp_getset = Module_getset, |
| 549 |
}; |
| 550 |
|
| 551 |
PyTypeObject MainModule_type = { |
| 552 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 553 |
.tp_name = "_drgn.MainModule", |
| 554 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 555 |
.tp_doc = drgn_MainModule_DOC, |
| 556 |
.tp_base = &Module_type, |
| 557 |
}; |
| 558 |
|
| 559 |
static PyObject *Module_get_info(Module *self, void *arg) |
| 560 |
{ |
| 561 |
return PyLong_FromUInt64(drgn_module_info(self->module)); |
| 562 |
} |
| 563 |
|
| 564 |
static PyGetSetDef SharedLibraryModule_getset[] = { |
| 565 |
{"dynamic_address", (getter)Module_get_info, NULL, |
| 566 |
drgn_SharedLibraryModule_dynamic_address_DOC}, |
| 567 |
{}, |
| 568 |
}; |
| 569 |
|
| 570 |
PyTypeObject SharedLibraryModule_type = { |
| 571 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 572 |
.tp_name = "_drgn.SharedLibraryModule", |
| 573 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 574 |
.tp_doc = drgn_SharedLibraryModule_DOC, |
| 575 |
.tp_getset = SharedLibraryModule_getset, |
| 576 |
.tp_base = &Module_type, |
| 577 |
}; |
| 578 |
|
| 579 |
static PyGetSetDef VdsoModule_getset[] = { |
| 580 |
{"dynamic_address", (getter)Module_get_info, NULL, |
| 581 |
drgn_VdsoModule_dynamic_address_DOC}, |
| 582 |
{}, |
| 583 |
}; |
| 584 |
|
| 585 |
PyTypeObject VdsoModule_type = { |
| 586 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 587 |
.tp_name = "_drgn.VdsoModule", |
| 588 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 589 |
.tp_doc = drgn_VdsoModule_DOC, |
| 590 |
.tp_getset = VdsoModule_getset, |
| 591 |
.tp_base = &Module_type, |
| 592 |
}; |
| 593 |
|
| 594 |
static PyObject *RelocatableModule_get_section_addresses(PyObject *self, |
| 595 |
void *arg) |
| 596 |
{ |
| 597 |
return PyObject_CallOneArg(ModuleSectionAddresses_class, self); |
| 598 |
} |
| 599 |
|
| 600 |
static PyGetSetDef RelocatableModule_getset[] = { |
| 601 |
{"address", (getter)Module_get_info, NULL, |
| 602 |
drgn_RelocatableModule_address_DOC}, |
| 603 |
{"section_addresses", RelocatableModule_get_section_addresses, |
| 604 |
NULL, drgn_RelocatableModule_section_addresses_DOC}, |
| 605 |
{}, |
| 606 |
}; |
| 607 |
|
| 608 |
PyTypeObject RelocatableModule_type = { |
| 609 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 610 |
.tp_name = "_drgn.RelocatableModule", |
| 611 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 612 |
.tp_doc = drgn_RelocatableModule_DOC, |
| 613 |
.tp_getset = RelocatableModule_getset, |
| 614 |
.tp_base = &Module_type, |
| 615 |
}; |
| 616 |
|
| 617 |
static PyGetSetDef ExtraModule_getset[] = { |
| 618 |
{"id", (getter)Module_get_info, NULL, drgn_ExtraModule_id_DOC}, |
| 619 |
{}, |
| 620 |
}; |
| 621 |
|
| 622 |
PyTypeObject ExtraModule_type = { |
| 623 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 624 |
.tp_name = "_drgn.ExtraModule", |
| 625 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 626 |
.tp_doc = drgn_ExtraModule_DOC, |
| 627 |
.tp_getset = ExtraModule_getset, |
| 628 |
.tp_base = &Module_type, |
| 629 |
}; |
| 630 |
|
| 631 |
static void ModuleIterator_dealloc(ModuleIterator *self) |
| 632 |
{ |
| 633 |
PyObject_GC_UnTrack(self); |
| 634 |
if (self->it) { |
| 635 |
struct drgn_program *prog = |
| 636 |
drgn_module_iterator_program(self->it); |
| 637 |
Py_DECREF(container_of(prog, Program, prog)); |
| 638 |
drgn_module_iterator_destroy(self->it); |
| 639 |
} |
| 640 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 641 |
} |
| 642 |
|
| 643 |
static int ModuleIterator_traverse(ModuleIterator *self, visitproc visit, |
| 644 |
void *arg) |
| 645 |
{ |
| 646 |
if (self->it) { |
| 647 |
struct drgn_program *prog = |
| 648 |
drgn_module_iterator_program(self->it); |
| 649 |
Py_VISIT(container_of(prog, Program, prog)); |
| 650 |
} |
| 651 |
return 0; |
| 652 |
} |
| 653 |
|
| 654 |
static PyObject *ModuleIterator_next(ModuleIterator *self) |
| 655 |
{ |
| 656 |
struct drgn_error *err; |
| 657 |
struct drgn_module *module; |
| 658 |
err = drgn_module_iterator_next(self->it, &module, NULL); |
| 659 |
if (err) |
| 660 |
return set_drgn_error(err); |
| 661 |
if (!module) |
| 662 |
return NULL; |
| 663 |
return Module_wrap(module); |
| 664 |
} |
| 665 |
|
| 666 |
static PyObject *ModuleIteratorWithNew_next(ModuleIterator *self) |
| 667 |
{ |
| 668 |
struct drgn_error *err; |
| 669 |
struct drgn_module *module; |
| 670 |
bool new; |
| 671 |
err = drgn_module_iterator_next(self->it, &module, &new); |
| 672 |
if (err) |
| 673 |
return set_drgn_error(err); |
| 674 |
if (!module) |
| 675 |
return NULL; |
| 676 |
return Py_BuildValue("NO", Module_wrap(module), |
| 677 |
new ? Py_True : Py_False); |
| 678 |
} |
| 679 |
|
| 680 |
PyTypeObject ModuleIterator_type = { |
| 681 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 682 |
.tp_name = "_drgn._ModuleIterator", |
| 683 |
.tp_basicsize = sizeof(ModuleIterator), |
| 684 |
.tp_dealloc = (destructor)ModuleIterator_dealloc, |
| 685 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 686 |
.tp_traverse = (traverseproc)ModuleIterator_traverse, |
| 687 |
.tp_iter = PyObject_SelfIter, |
| 688 |
.tp_iternext = (iternextfunc)ModuleIterator_next, |
| 689 |
}; |
| 690 |
|
| 691 |
PyTypeObject ModuleIteratorWithNew_type = { |
| 692 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 693 |
.tp_name = "_drgn._ModuleIteratorWithNew", |
| 694 |
.tp_basicsize = sizeof(ModuleIterator), |
| 695 |
.tp_dealloc = (destructor)ModuleIterator_dealloc, |
| 696 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 697 |
.tp_traverse = (traverseproc)ModuleIterator_traverse, |
| 698 |
.tp_iter = PyObject_SelfIter, |
| 699 |
.tp_iternext = (iternextfunc)ModuleIteratorWithNew_next, |
| 700 |
}; |