| 1 |
// Copyright (c) 2024 Oracle and/or its affiliates |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include "drgnpy.h" |
| 5 |
#include "../symbol.h" |
| 6 |
|
| 7 |
static void SymbolIndex_dealloc(SymbolIndex *self) |
| 8 |
{ |
| 9 |
drgn_symbol_index_deinit(&self->index); |
| 10 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 11 |
} |
| 12 |
|
| 13 |
static PyObject *SymbolIndex_call(SymbolIndex *self, PyObject *args, PyObject *kwargs) |
| 14 |
{ |
| 15 |
PyObject *prog_obj; |
| 16 |
struct index_arg address = { .allow_none = true }; |
| 17 |
const char *name; |
| 18 |
static char *kwnames[] = {"prog", "name", "address", "one", NULL}; |
| 19 |
int single; // 'p' format specifier expects an int, not bool |
| 20 |
|
| 21 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OzO&p:__call__", kwnames, |
| 22 |
&prog_obj, &name, index_converter, &address, |
| 23 |
&single)) |
| 24 |
return NULL; |
| 25 |
|
| 26 |
unsigned int flags = 0; |
| 27 |
if (single) |
| 28 |
flags |= DRGN_FIND_SYMBOL_ONE; |
| 29 |
if (!address.is_none) |
| 30 |
flags |= DRGN_FIND_SYMBOL_ADDR; |
| 31 |
if (name) |
| 32 |
flags |= DRGN_FIND_SYMBOL_NAME; |
| 33 |
|
| 34 |
struct drgn_symbol_result_builder builder; |
| 35 |
drgn_symbol_result_builder_init(&builder, flags & DRGN_FIND_SYMBOL_ONE); |
| 36 |
|
| 37 |
struct drgn_error *err = |
| 38 |
drgn_symbol_index_find(name, address.uvalue, flags, &self->index, &builder); |
| 39 |
if (err) |
| 40 |
goto error; |
| 41 |
|
| 42 |
/* We return a list regardless */ |
| 43 |
if (single) { |
| 44 |
struct drgn_symbol *symbol = drgn_symbol_result_builder_single(&builder); |
| 45 |
_cleanup_pydecref_ PyObject *list = PyList_New(symbol ? 1 : 0); |
| 46 |
if (!list) |
| 47 |
goto error; |
| 48 |
if (symbol) { |
| 49 |
PyObject *pysym = Symbol_wrap(symbol, (PyObject *)self); |
| 50 |
if (!pysym) |
| 51 |
goto error; |
| 52 |
PyList_SET_ITEM(list, 0, pysym); |
| 53 |
} |
| 54 |
return_ptr(list); |
| 55 |
} else { |
| 56 |
struct drgn_symbol **syms; |
| 57 |
size_t count; |
| 58 |
drgn_symbol_result_builder_array(&builder, &syms, &count); |
| 59 |
return Symbol_list_wrap(syms, count, (PyObject *)self); |
| 60 |
} |
| 61 |
|
| 62 |
return NULL; |
| 63 |
error: |
| 64 |
drgn_symbol_result_builder_abort(&builder); |
| 65 |
return err ? set_drgn_error(err) : NULL; |
| 66 |
} |
| 67 |
|
| 68 |
static PyObject *SymbolIndex_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) |
| 69 |
{ |
| 70 |
static char *kwnames[] = {"symbols", NULL}; |
| 71 |
PyObject *list_obj; |
| 72 |
|
| 73 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwnames, &list_obj)) |
| 74 |
return NULL; |
| 75 |
|
| 76 |
_cleanup_pydecref_ PyObject *iter = |
| 77 |
PyObject_GetIter(list_obj); |
| 78 |
if (!iter) |
| 79 |
return NULL; |
| 80 |
|
| 81 |
_cleanup_(drgn_symbol_index_builder_deinit) |
| 82 |
struct drgn_symbol_index_builder builder; |
| 83 |
drgn_symbol_index_builder_init(&builder); |
| 84 |
|
| 85 |
for (;;) { |
| 86 |
_cleanup_pydecref_ PyObject *item = PyIter_Next(iter); |
| 87 |
if (!item) |
| 88 |
break; |
| 89 |
if (!PyObject_TypeCheck(item, &Symbol_type)) |
| 90 |
return PyErr_Format(PyExc_TypeError, "expected sequence of Symbols"); |
| 91 |
Symbol *sym = (Symbol *)item; |
| 92 |
if (!drgn_symbol_index_builder_add(&builder, sym->sym)) |
| 93 |
return PyErr_NoMemory(); |
| 94 |
} |
| 95 |
|
| 96 |
if (PyErr_Occurred()) |
| 97 |
return NULL; |
| 98 |
|
| 99 |
_cleanup_pydecref_ SymbolIndex *index_obj = call_tp_alloc(SymbolIndex); |
| 100 |
if (!index_obj) |
| 101 |
return NULL; |
| 102 |
|
| 103 |
struct drgn_error *err = |
| 104 |
drgn_symbol_index_init_from_builder(&index_obj->index, |
| 105 |
&builder); |
| 106 |
// On error, the builder and index are already deinitialized |
| 107 |
if (err) |
| 108 |
return set_drgn_error(err); |
| 109 |
|
| 110 |
return (PyObject *)no_cleanup_ptr(index_obj); |
| 111 |
} |
| 112 |
|
| 113 |
PyTypeObject SymbolIndex_type = { |
| 114 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 115 |
.tp_name = "_drgn.SymbolIndex", |
| 116 |
.tp_basicsize = sizeof(SymbolIndex), |
| 117 |
.tp_dealloc = (destructor)SymbolIndex_dealloc, |
| 118 |
// Doesn't reference any objects, no GC needed. |
| 119 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 120 |
.tp_doc = drgn_SymbolIndex_DOC, |
| 121 |
.tp_call = (ternaryfunc)SymbolIndex_call, |
| 122 |
.tp_new = SymbolIndex_new, |
| 123 |
}; |