| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include "drgnpy.h" |
| 5 |
#include "../error.h" |
| 6 |
|
| 7 |
PyObject *MemorySearchIterator_wrap(PyTypeObject *type, |
| 8 |
struct drgn_memory_search_iterator *it) |
| 9 |
{ |
| 10 |
MemorySearchIterator *ret = |
| 11 |
(MemorySearchIterator *)type->tp_alloc(type, 0); |
| 12 |
if (!ret) |
| 13 |
return NULL; |
| 14 |
struct drgn_program *prog = drgn_memory_search_iterator_program(it); |
| 15 |
Py_INCREF(container_of(prog, Program, prog)); |
| 16 |
ret->it = it; |
| 17 |
return (PyObject *)ret; |
| 18 |
} |
| 19 |
|
| 20 |
static void MemorySearchIterator_dealloc(MemorySearchIterator *self) |
| 21 |
{ |
| 22 |
PyObject_GC_UnTrack(self); |
| 23 |
if (self->it) { |
| 24 |
struct drgn_program *prog = |
| 25 |
drgn_memory_search_iterator_program(self->it); |
| 26 |
drgn_memory_search_iterator_destroy(self->it); |
| 27 |
Py_DECREF(container_of(prog, Program, prog)); |
| 28 |
} |
| 29 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 30 |
} |
| 31 |
|
| 32 |
static int MemorySearchIterator_traverse(MemorySearchIterator *self, |
| 33 |
visitproc visit, void *arg) |
| 34 |
{ |
| 35 |
if (self->it) { |
| 36 |
struct drgn_program *prog = |
| 37 |
drgn_memory_search_iterator_program(self->it); |
| 38 |
Py_VISIT(container_of(prog, Program, prog)); |
| 39 |
} |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
static PyObject *MemorySearchIterator_next(MemorySearchIterator *self) |
| 44 |
{ |
| 45 |
struct drgn_error *err; |
| 46 |
uint64_t address; |
| 47 |
err = drgn_memory_search_iterator_next(self->it, &address, NULL, NULL); |
| 48 |
if (drgn_error_catch(&err, DRGN_ERROR_STOP)) |
| 49 |
return NULL; |
| 50 |
else if (err) |
| 51 |
return set_drgn_error(err); |
| 52 |
return PyLong_FromUInt64(address); |
| 53 |
} |
| 54 |
|
| 55 |
static PyObject *MemorySearchIteratorWithBytes_next(MemorySearchIterator *self) |
| 56 |
{ |
| 57 |
struct drgn_error *err; |
| 58 |
uint64_t address; |
| 59 |
const void *match; |
| 60 |
size_t match_len; |
| 61 |
err = drgn_memory_search_iterator_next(self->it, &address, &match, |
| 62 |
&match_len); |
| 63 |
if (drgn_error_catch(&err, DRGN_ERROR_STOP)) |
| 64 |
return NULL; |
| 65 |
else if (err) |
| 66 |
return set_drgn_error(err); |
| 67 |
_cleanup_pydecref_ PyObject *ret = PyTuple_New(2); |
| 68 |
if (ret) { |
| 69 |
PyObject *tmp = PyLong_FromUInt64(address); |
| 70 |
if (!tmp) |
| 71 |
return NULL; |
| 72 |
PyTuple_SET_ITEM(ret, 0, tmp); |
| 73 |
|
| 74 |
tmp = PyBytes_FromStringAndSize(match, match_len); |
| 75 |
if (!tmp) |
| 76 |
return NULL; |
| 77 |
PyTuple_SET_ITEM(ret, 1, tmp); |
| 78 |
} |
| 79 |
return_ptr(ret); |
| 80 |
} |
| 81 |
|
| 82 |
static PyObject *MemorySearchIteratorWithStr_next(MemorySearchIterator *self) |
| 83 |
{ |
| 84 |
struct drgn_error *err; |
| 85 |
uint64_t address; |
| 86 |
const void *match; |
| 87 |
size_t match_len; |
| 88 |
err = drgn_memory_search_iterator_next(self->it, &address, &match, |
| 89 |
&match_len); |
| 90 |
if (drgn_error_catch(&err, DRGN_ERROR_STOP)) |
| 91 |
return NULL; |
| 92 |
else if (err) |
| 93 |
return set_drgn_error(err); |
| 94 |
_cleanup_pydecref_ PyObject *ret = PyTuple_New(2); |
| 95 |
if (ret) { |
| 96 |
PyObject *tmp = PyLong_FromUInt64(address); |
| 97 |
if (!tmp) |
| 98 |
return NULL; |
| 99 |
PyTuple_SET_ITEM(ret, 0, tmp); |
| 100 |
|
| 101 |
tmp = PyUnicode_FromStringAndSize(match, match_len); |
| 102 |
if (!tmp) |
| 103 |
return NULL; |
| 104 |
PyTuple_SET_ITEM(ret, 1, tmp); |
| 105 |
} |
| 106 |
return_ptr(ret); |
| 107 |
} |
| 108 |
|
| 109 |
static PyObject *MemorySearchIteratorWithInt_next(MemorySearchIterator *self) |
| 110 |
{ |
| 111 |
struct drgn_error *err; |
| 112 |
uint64_t address; |
| 113 |
const void *match; |
| 114 |
size_t match_len; |
| 115 |
err = drgn_memory_search_iterator_next(self->it, &address, &match, |
| 116 |
&match_len); |
| 117 |
if (drgn_error_catch(&err, DRGN_ERROR_STOP)) |
| 118 |
return NULL; |
| 119 |
else if (err) |
| 120 |
return set_drgn_error(err); |
| 121 |
_cleanup_pydecref_ PyObject *ret = PyTuple_New(2); |
| 122 |
if (ret) { |
| 123 |
PyObject *tmp = PyLong_FromUInt64(address); |
| 124 |
if (!tmp) |
| 125 |
return NULL; |
| 126 |
PyTuple_SET_ITEM(ret, 0, tmp); |
| 127 |
|
| 128 |
struct drgn_program *prog = |
| 129 |
drgn_memory_search_iterator_program(self->it); |
| 130 |
// We must know the byte order if we did an int search, so no |
| 131 |
// need to check has_platform. |
| 132 |
bool bswap = drgn_platform_bswap(&prog->platform); |
| 133 |
if (0) {} |
| 134 |
#define X(bits) \ |
| 135 |
else if (match_len == sizeof(uint##bits##_t)) { \ |
| 136 |
uint##bits##_t value; \ |
| 137 |
memcpy(&value, match, sizeof(value)); \ |
| 138 |
if (bswap) \ |
| 139 |
value = bswap_##bits(value); \ |
| 140 |
tmp = PyLong_FromUInt##bits(value); \ |
| 141 |
} |
| 142 |
SEARCH_MEMORY_UINT_SIZES |
| 143 |
#undef X |
| 144 |
else |
| 145 |
assert(false); |
| 146 |
if (!tmp) |
| 147 |
return NULL; |
| 148 |
PyTuple_SET_ITEM(ret, 1, tmp); |
| 149 |
} |
| 150 |
return_ptr(ret); |
| 151 |
} |
| 152 |
|
| 153 |
static MemorySearchIterator * |
| 154 |
MemorySearchIterator_set_address_range(MemorySearchIterator *self, |
| 155 |
PyObject *args, PyObject *kwds) |
| 156 |
{ |
| 157 |
struct drgn_error *err; |
| 158 |
static char *keywords[] = { |
| 159 |
"min_address", "max_address", "physical", NULL |
| 160 |
}; |
| 161 |
PyObject *min_address_obj = Py_None; |
| 162 |
PyObject *max_address_obj = Py_None; |
| 163 |
int physical = 0; |
| 164 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOp:set_address_range", |
| 165 |
keywords, &min_address_obj, |
| 166 |
&max_address_obj, &physical)) |
| 167 |
return NULL; |
| 168 |
|
| 169 |
uint64_t min_address = 0; |
| 170 |
if (min_address_obj != Py_None |
| 171 |
&& PyLong_AsUInt64(min_address_obj, &min_address)) |
| 172 |
return NULL; |
| 173 |
uint64_t max_address = UINT64_MAX; |
| 174 |
if (max_address_obj != Py_None |
| 175 |
&& PyLong_AsUInt64(max_address_obj, &max_address)) |
| 176 |
return NULL; |
| 177 |
err = drgn_memory_search_iterator_set_address_range(self->it, |
| 178 |
min_address, |
| 179 |
max_address, |
| 180 |
physical); |
| 181 |
if (err) |
| 182 |
return set_drgn_error(err); |
| 183 |
Py_INCREF(self); |
| 184 |
return self; |
| 185 |
} |
| 186 |
|
| 187 |
static PyMethodDef MemorySearchIterator_methods[] = { |
| 188 |
{"set_address_range", |
| 189 |
(PyCFunction)MemorySearchIterator_set_address_range, |
| 190 |
METH_VARARGS | METH_KEYWORDS, |
| 191 |
drgn_MemorySearchIterator_set_address_range_DOC}, |
| 192 |
{}, |
| 193 |
}; |
| 194 |
|
| 195 |
PyTypeObject MemorySearchIterator_type = { |
| 196 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 197 |
.tp_name = "_drgn._MemorySearchIterator", |
| 198 |
.tp_basicsize = sizeof(MemorySearchIterator), |
| 199 |
.tp_dealloc = (destructor)MemorySearchIterator_dealloc, |
| 200 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 201 |
.tp_traverse = (traverseproc)MemorySearchIterator_traverse, |
| 202 |
.tp_iter = PyObject_SelfIter, |
| 203 |
.tp_iternext = (iternextfunc)MemorySearchIterator_next, |
| 204 |
.tp_methods = MemorySearchIterator_methods, |
| 205 |
}; |
| 206 |
|
| 207 |
PyTypeObject MemorySearchIteratorWithBytes_type = { |
| 208 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 209 |
.tp_name = "_drgn._MemorySearchIteratorWithBytes", |
| 210 |
.tp_basicsize = sizeof(MemorySearchIterator), |
| 211 |
.tp_dealloc = (destructor)MemorySearchIterator_dealloc, |
| 212 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 213 |
.tp_traverse = (traverseproc)MemorySearchIterator_traverse, |
| 214 |
.tp_iter = PyObject_SelfIter, |
| 215 |
.tp_iternext = (iternextfunc)MemorySearchIteratorWithBytes_next, |
| 216 |
.tp_methods = MemorySearchIterator_methods, |
| 217 |
}; |
| 218 |
|
| 219 |
PyTypeObject MemorySearchIteratorWithStr_type = { |
| 220 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 221 |
.tp_name = "_drgn._MemorySearchIteratorWithStr", |
| 222 |
.tp_basicsize = sizeof(MemorySearchIterator), |
| 223 |
.tp_dealloc = (destructor)MemorySearchIterator_dealloc, |
| 224 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 225 |
.tp_traverse = (traverseproc)MemorySearchIterator_traverse, |
| 226 |
.tp_iter = PyObject_SelfIter, |
| 227 |
.tp_iternext = (iternextfunc)MemorySearchIteratorWithStr_next, |
| 228 |
.tp_methods = MemorySearchIterator_methods, |
| 229 |
}; |
| 230 |
|
| 231 |
PyTypeObject MemorySearchIteratorWithInt_type = { |
| 232 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 233 |
.tp_name = "_drgn._MemorySearchIteratorWithInt", |
| 234 |
.tp_basicsize = sizeof(MemorySearchIterator), |
| 235 |
.tp_dealloc = (destructor)MemorySearchIterator_dealloc, |
| 236 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 237 |
.tp_traverse = (traverseproc)MemorySearchIterator_traverse, |
| 238 |
.tp_iter = PyObject_SelfIter, |
| 239 |
.tp_iternext = (iternextfunc)MemorySearchIteratorWithInt_next, |
| 240 |
.tp_methods = MemorySearchIterator_methods, |
| 241 |
}; |