2021-11-21 23:59:44 +00:00
|
|
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
2022-11-02 00:05:16 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
2019-07-29 08:57:28 +01:00
|
|
|
|
|
|
|
#include "drgnpy.h"
|
|
|
|
|
|
|
|
PyObject *Platform_wrap(const struct drgn_platform *platform)
|
|
|
|
{
|
|
|
|
struct drgn_error *err;
|
|
|
|
struct drgn_platform *tmp;
|
|
|
|
err = drgn_platform_create(drgn_platform_arch(platform),
|
|
|
|
drgn_platform_flags(platform),
|
|
|
|
&tmp);
|
|
|
|
if (err)
|
|
|
|
return set_drgn_error(err);
|
2022-08-29 21:37:30 +01:00
|
|
|
Platform *ret = call_tp_alloc(Platform);
|
2019-07-29 08:57:28 +01:00
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
ret->platform = tmp;
|
|
|
|
return (PyObject *)ret;
|
|
|
|
}
|
|
|
|
|
2022-02-17 18:45:42 +00:00
|
|
|
static Platform *Platform_new(PyTypeObject *subtype, PyObject *args,
|
|
|
|
PyObject *kwds)
|
2019-07-29 08:57:28 +01:00
|
|
|
{
|
|
|
|
static char *keywords[] = {"arch", "flags", NULL};
|
|
|
|
struct enum_arg arch = { .type = Architecture_class, };
|
|
|
|
struct enum_arg flags = {
|
|
|
|
.type = PlatformFlags_class,
|
|
|
|
.value = DRGN_PLATFORM_DEFAULT_FLAGS,
|
|
|
|
.allow_none = true,
|
|
|
|
};
|
|
|
|
struct drgn_error *err;
|
|
|
|
struct drgn_platform *platform;
|
|
|
|
Platform *ret;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&:Platform", keywords,
|
|
|
|
enum_converter, &arch, enum_converter,
|
|
|
|
&flags))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
err = drgn_platform_create(arch.value, flags.value, &platform);
|
|
|
|
if (err)
|
|
|
|
return set_drgn_error(err);
|
|
|
|
ret = (Platform *)subtype->tp_alloc(subtype, 0);
|
|
|
|
if (!ret) {
|
|
|
|
drgn_platform_destroy(platform);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret->platform = platform;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Platform_dealloc(Platform *self)
|
|
|
|
{
|
|
|
|
drgn_platform_destroy(self->platform);
|
|
|
|
Py_TYPE(self)->tp_free((PyObject *)self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *Platform_richcompare(Platform *self, PyObject *other, int op)
|
|
|
|
{
|
|
|
|
if (!PyObject_TypeCheck(other, &Platform_type) ||
|
|
|
|
(op != Py_EQ && op != Py_NE))
|
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
2021-01-28 19:32:24 +00:00
|
|
|
bool ret = drgn_platform_eq(self->platform,
|
|
|
|
((Platform *)other)->platform);
|
2019-07-29 08:57:28 +01:00
|
|
|
if (op == Py_NE)
|
|
|
|
ret = !ret;
|
2021-01-28 19:32:24 +00:00
|
|
|
Py_RETURN_BOOL(ret);
|
2019-07-29 08:57:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *Platform_get_arch(Platform *self, void *arg)
|
|
|
|
{
|
|
|
|
return PyObject_CallFunction(Architecture_class, "k",
|
|
|
|
(unsigned long)drgn_platform_arch(self->platform));
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *Platform_get_flags(Platform *self, void *arg)
|
|
|
|
{
|
|
|
|
return PyObject_CallFunction(PlatformFlags_class, "k",
|
|
|
|
(unsigned long)drgn_platform_flags(self->platform));
|
|
|
|
}
|
|
|
|
|
2019-10-18 10:03:32 +01:00
|
|
|
static PyObject *Platform_get_registers(Platform *self, void *arg)
|
|
|
|
{
|
2021-01-28 23:49:21 +00:00
|
|
|
size_t num_registers = drgn_platform_num_registers(self->platform);
|
2023-08-02 16:37:09 +01:00
|
|
|
_cleanup_pydecref_ PyObject *tuple = PyTuple_New(num_registers);
|
2019-10-18 10:03:32 +01:00
|
|
|
if (!tuple)
|
|
|
|
return NULL;
|
2021-01-28 23:49:21 +00:00
|
|
|
for (size_t i = 0; i < num_registers; i++) {
|
|
|
|
const struct drgn_register *reg =
|
|
|
|
drgn_platform_register(self->platform, i);
|
2022-08-29 21:37:30 +01:00
|
|
|
Register *item = call_tp_alloc(Register);
|
2023-08-02 16:37:09 +01:00
|
|
|
if (!item)
|
2019-10-18 10:03:32 +01:00
|
|
|
return NULL;
|
2022-08-29 21:37:30 +01:00
|
|
|
item->reg = reg;
|
|
|
|
PyTuple_SET_ITEM(tuple, i, (PyObject *)item);
|
2019-10-18 10:03:32 +01:00
|
|
|
}
|
2023-08-02 16:37:09 +01:00
|
|
|
return_ptr(tuple);
|
2019-10-18 10:03:32 +01:00
|
|
|
}
|
|
|
|
|
2019-07-29 08:57:28 +01:00
|
|
|
static PyObject *Platform_repr(Platform *self)
|
|
|
|
{
|
2023-08-02 16:37:09 +01:00
|
|
|
_cleanup_pydecref_ PyObject *arch_obj = Platform_get_arch(self, NULL);
|
2019-07-29 08:57:28 +01:00
|
|
|
if (!arch_obj)
|
|
|
|
return NULL;
|
2023-08-02 16:37:09 +01:00
|
|
|
_cleanup_pydecref_ PyObject *flags_obj = Platform_get_flags(self, NULL);
|
|
|
|
if (!flags_obj)
|
2019-07-29 08:57:28 +01:00
|
|
|
return NULL;
|
2023-08-02 16:37:09 +01:00
|
|
|
return PyUnicode_FromFormat("Platform(%R, %R)", arch_obj, flags_obj);
|
2019-07-29 08:57:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef Platform_getset[] = {
|
|
|
|
{"arch", (getter)Platform_get_arch, NULL, drgn_Platform_arch_DOC},
|
|
|
|
{"flags", (getter)Platform_get_flags, NULL, drgn_Platform_flags_DOC},
|
2019-10-18 10:03:32 +01:00
|
|
|
{"registers", (getter)Platform_get_registers, NULL,
|
|
|
|
drgn_Platform_registers_DOC},
|
2019-07-29 08:57:28 +01:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
PyTypeObject Platform_type = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2019-11-15 07:16:29 +00:00
|
|
|
.tp_name = "_drgn.Platform",
|
|
|
|
.tp_basicsize = sizeof(Platform),
|
|
|
|
.tp_dealloc = (destructor)Platform_dealloc,
|
|
|
|
.tp_repr = (reprfunc)Platform_repr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
|
|
.tp_doc = drgn_Platform_DOC,
|
|
|
|
.tp_richcompare = (richcmpfunc)Platform_richcompare,
|
|
|
|
.tp_getset = Platform_getset,
|
|
|
|
.tp_new = (newfunc)Platform_new,
|
2019-07-29 08:57:28 +01:00
|
|
|
};
|
2019-10-18 10:03:32 +01:00
|
|
|
|
2021-01-28 23:49:21 +00:00
|
|
|
static PyObject *Register_richcompare(Register *self, PyObject *other, int op)
|
|
|
|
{
|
|
|
|
if (!PyObject_TypeCheck(other, &Register_type) ||
|
|
|
|
(op != Py_EQ && op != Py_NE))
|
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
|
|
|
bool ret = self->reg == ((Register *)other)->reg;
|
|
|
|
if (op == Py_NE)
|
|
|
|
ret = !ret;
|
|
|
|
Py_RETURN_BOOL(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *Register_get_names(Register *self, void *arg)
|
|
|
|
{
|
|
|
|
size_t num_names;
|
|
|
|
const char * const *names = drgn_register_names(self->reg, &num_names);
|
2023-08-02 16:37:09 +01:00
|
|
|
_cleanup_pydecref_ PyObject *ret = PyTuple_New(num_names);
|
2021-01-28 23:49:21 +00:00
|
|
|
for (size_t i = 0; i < num_names; i++) {
|
|
|
|
PyObject *item = PyUnicode_FromString(names[i]);
|
2023-08-02 16:37:09 +01:00
|
|
|
if (!item)
|
2021-01-28 23:49:21 +00:00
|
|
|
return NULL;
|
|
|
|
PyTuple_SET_ITEM(ret, i, item);
|
|
|
|
}
|
2023-08-02 16:37:09 +01:00
|
|
|
return_ptr(ret);
|
2021-01-28 23:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *Register_repr(Register *self)
|
|
|
|
{
|
2023-08-02 16:37:09 +01:00
|
|
|
_cleanup_pydecref_ PyObject *names_obj = Register_get_names(self, NULL);
|
2021-01-28 23:49:21 +00:00
|
|
|
if (!names_obj)
|
|
|
|
return NULL;
|
2023-08-02 16:37:09 +01:00
|
|
|
return PyUnicode_FromFormat("Register(%R)", names_obj);
|
2021-01-28 23:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef Register_getset[] = {
|
|
|
|
{"names", (getter)Register_get_names, NULL, drgn_Register_names_DOC},
|
2019-10-18 10:03:32 +01:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
2021-01-28 23:49:21 +00:00
|
|
|
PyTypeObject Register_type = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
.tp_name = "_drgn.Register",
|
|
|
|
.tp_basicsize = sizeof(Register),
|
|
|
|
.tp_repr = (reprfunc)Register_repr,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
|
|
.tp_doc = drgn_Register_DOC,
|
|
|
|
.tp_richcompare = (richcmpfunc)Register_richcompare,
|
|
|
|
.tp_getset = Register_getset,
|
2019-10-18 10:03:32 +01:00
|
|
|
};
|