Skip to content

Pytarget py3 support #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions gdb/python/py-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ PyObject* DictIter_iternext(PyObject *self)
}

static PyTypeObject DictIterType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
PyVarObject_HEAD_INIT (NULL, 0)
"gdb._DictIter", /*tp_name*/
sizeof(DictIter), /*tp_basicsize*/
0, /*tp_itemsize*/
Expand All @@ -174,8 +173,7 @@ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
DictIter_iter, /* tp_iter: __iter__() method */
DictIter_iternext /* tp_iternext: next() method */,
.tp_new = PyType_GenericNew
DictIter_iternext /* tp_iternext: next() method */
};

static PyObject *
Expand Down Expand Up @@ -538,6 +536,7 @@ gdbpy_initialize_blocks (void)
if (PyType_Ready (&block_syms_iterator_object_type) < 0)
return -1;

DictIterType.tp_new = PyType_GenericNew;
if (PyType_Ready(&DictIterType) < 0) return -1;
/* Register an objfile "free" callback so we can properly
invalidate blocks when an object file is about to be
Expand Down
34 changes: 29 additions & 5 deletions gdb/python/py-minsymbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,23 +319,43 @@ gdbpy_lookup_minimal_symbol (PyObject *self, PyObject *args, PyObject *kw)
static char *keywords[] = { "name", "sfile", "objfile", NULL };
struct bound_minimal_symbol bound_minsym = {};
PyObject *msym_obj = NULL, *sfile_obj = NULL, *objfile_obj = NULL;
#if PY_MAJOR_VERSION >= 3
PyObject *temp = NULL;
#endif

if (!PyArg_ParseTupleAndKeywords (args, kw, "s|OO", keywords, &name,
&sfile_obj, &objfile_obj))
return NULL;

if (sfile_obj && sfile_obj != Py_None)
{
sfile = PyString_AsString (sfile_obj);
if (!sfile)
return NULL;
#if PY_MAJOR_VERSION >= 3
temp = PyUnicode_AsASCIIString(sfile_obj);
if (!temp)
return NULL;

sfile = PyBytes_AsString(temp);
#else
sfile = PyString_AsString(sfile_obj);
#endif

if (!sfile) {
#if PY_MAJOR_VERSION >= 3
Py_DECREF(temp);
#endif
return NULL;
}
}

if (objfile_obj && objfile_obj != Py_None)
{
objfile = objfpy_object_to_objfile (objfile_obj);
if (!objfile)
return NULL;
if (!objfile) {
#if PY_MAJOR_VERSION >= 3
Py_DECREF(temp);
#endif
return NULL;
}
}

TRY
Expand All @@ -348,6 +368,10 @@ gdbpy_lookup_minimal_symbol (PyObject *self, PyObject *args, PyObject *kw)
}
END_CATCH

#if PY_MAJOR_VERSION >= 3
Py_XDECREF(temp);
#endif

if (bound_minsym.minsym)
msym_obj = bound_minsym_to_minsym_object (&bound_minsym);

Expand Down
2 changes: 2 additions & 0 deletions gdb/python/py-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ register_set_value(PyObject *self, PyObject *value_obj, void *closure)
ret = write_register (regcache, obj->regnum, &ul_value);
}
}
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check (value_obj))
{
ul_value = PyInt_AsUnsignedLongMask (value_obj);
Expand All @@ -216,6 +217,7 @@ register_set_value(PyObject *self, PyObject *value_obj, void *closure)
ret = write_register (regcache, obj->regnum, &ul_value);
}
}
#endif
else
{
value = value_object_to_value(value_obj);
Expand Down
75 changes: 38 additions & 37 deletions gdb/python/py-target.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
static PyObject *py_target_xfer_eof_error;
static PyObject *py_target_xfer_unavailable_error;

extern PyTypeObject target_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("target_object");
extern PyTypeObject pytarget_object_type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("pytarget_object");

/* Require that Target operations are valid */
#define THPY_REQUIRE_VALID_RETURN(Target, ret) \
Expand Down Expand Up @@ -96,16 +96,16 @@ extern PyTypeObject target_object_type
* This gives us the opportunity to find our Python Object when we are called
* from C code
*/
static target_object * target_ops_to_target_obj(struct target_ops *ops)
static pytarget_object * target_ops_to_target_obj(struct target_ops *ops)
{
target_object *target_obj;
pytarget_object *target_obj;

if (ops->to_data == &target_object_type) {
target_obj = container_of(ops, target_object, python_ops);
if (ops->to_data == &pytarget_object_type) {
target_obj = container_of(ops, pytarget_object, python_ops);
return target_obj;
}

target_obj = PyObject_New (target_object, &target_object_type);
target_obj = PyObject_New (pytarget_object, &pytarget_object_type);
if (target_obj)
target_obj->ops = ops;

Expand Down Expand Up @@ -135,7 +135,7 @@ static const
char *py_target_to_thread_name (struct target_ops *ops,
struct thread_info *info)
{
target_object *target_obj = target_ops_to_target_obj (ops);
pytarget_object *target_obj = target_ops_to_target_obj (ops);
PyObject *self = (PyObject *) target_obj;
PyObject *arglist = NULL;
PyObject *result = NULL;
Expand Down Expand Up @@ -196,7 +196,7 @@ py_target_to_xfer_partial (struct target_ops *ops,
gdb_byte *gdb_readbuf, const gdb_byte *gdb_writebuf,
ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
{
target_object *target_obj = target_ops_to_target_obj (ops);
pytarget_object *target_obj = target_ops_to_target_obj (ops);
PyObject *self = (PyObject *) target_obj;
PyObject *callback = NULL;
PyObject *readbuf = NULL;
Expand Down Expand Up @@ -306,7 +306,7 @@ static char *
py_target_to_extra_thread_info (struct target_ops *ops, struct thread_info *info)
{
/* Note how we can obtain our Parent Python Object from the ops too */
target_object *target_obj = target_ops_to_target_obj(ops);
pytarget_object *target_obj = target_ops_to_target_obj(ops);
PyObject * self = (PyObject *)target_obj;

struct cleanup *cleanup;
Expand All @@ -322,7 +322,7 @@ py_target_to_extra_thread_info (struct target_ops *ops, struct thread_info *info
static void
py_target_to_update_thread_list (struct target_ops *ops)
{
target_object *target_obj = target_ops_to_target_obj (ops);
pytarget_object *target_obj = target_ops_to_target_obj (ops);
PyObject * self = (PyObject *) target_obj;
PyObject *callback = NULL;
PyObject *arglist = NULL;
Expand Down Expand Up @@ -363,7 +363,7 @@ py_target_to_update_thread_list (struct target_ops *ops)
static int
py_target_to_thread_alive (struct target_ops *ops, ptid_t ptid)
{
target_object *target_obj = target_ops_to_target_obj (ops);
pytarget_object *target_obj = target_ops_to_target_obj (ops);
PyObject *self = (PyObject *) target_obj;
PyObject *ptid_obj = NULL;
PyObject *arglist = NULL;
Expand Down Expand Up @@ -421,7 +421,7 @@ py_target_to_thread_alive (struct target_ops *ops, ptid_t ptid)
static char *py_target_to_pid_to_str(struct target_ops *ops, ptid_t ptid)
{
/* Note how we can obtain our Parent Python Object from the ops too */
target_object *target_obj = target_ops_to_target_obj (ops);
pytarget_object *target_obj = target_ops_to_target_obj (ops);
PyObject *self = (PyObject *) target_obj;
PyObject *ptid_obj = NULL;
PyObject *arglist = NULL;
Expand Down Expand Up @@ -477,7 +477,7 @@ static char *py_target_to_pid_to_str(struct target_ops *ops, ptid_t ptid)
static void py_target_to_fetch_registers (struct target_ops *ops,
struct regcache *regcache, int reg)
{
target_object *target_obj = target_ops_to_target_obj (ops);
pytarget_object *target_obj = target_ops_to_target_obj (ops);
PyObject * self = (PyObject *) target_obj;
PyObject *arglist = NULL;
PyObject *result = NULL;
Expand Down Expand Up @@ -529,7 +529,7 @@ static void py_target_to_fetch_registers (struct target_ops *ops,
static void py_target_to_prepare_to_store (struct target_ops *ops,
struct regcache *regcache)
{
target_object *target_obj = target_ops_to_target_obj (ops);
pytarget_object *target_obj = target_ops_to_target_obj (ops);
PyObject * self = (PyObject *) target_obj;
PyObject *arglist = NULL;
PyObject *result = NULL;
Expand Down Expand Up @@ -574,7 +574,7 @@ static void py_target_to_prepare_to_store (struct target_ops *ops,
static void py_target_to_store_registers (struct target_ops *ops,
struct regcache *regcache, int reg)
{
target_object *target_obj = target_ops_to_target_obj (ops);
pytarget_object *target_obj = target_ops_to_target_obj (ops);
PyObject * self = (PyObject *) target_obj;
PyObject *arglist = NULL;
PyObject *result = NULL;
Expand Down Expand Up @@ -625,7 +625,7 @@ static void py_target_to_store_registers (struct target_ops *ops,
static int
py_target_to_has_execution (struct target_ops *ops, ptid_t ptid)
{
target_object *target_obj = target_ops_to_target_obj (ops);
pytarget_object *target_obj = target_ops_to_target_obj (ops);
PyObject * self = (PyObject *) target_obj;
PyObject *arglist = NULL;
PyObject *result = NULL;
Expand Down Expand Up @@ -712,7 +712,7 @@ static void py_target_register_ops(struct target_ops * ops)

ops->to_magic = OPS_MAGIC;

ops->to_data = &target_object_type;
ops->to_data = &pytarget_object_type;

/* Install any remaining operations as delegators */
complete_target_initialization (ops);
Expand All @@ -732,7 +732,7 @@ target_dealloc (PyObject *self)
{
ENTRY();

// Py_DECREF (((target_object *) self)->inf_obj);
// Py_DECREF (((pytarget_object *) self)->inf_obj);
// Decremement any references taken....
Py_TYPE (self)->tp_free (self);

Expand All @@ -748,10 +748,11 @@ enum target_names {
static PyObject *
tgt_py_get_name (PyObject *self, void * arg)
{
enum target_names target_string = (enum target_names) arg;
target_object *target_obj = (target_object *) self;
enum target_names target_string = (enum target_names) (intptr_t)arg;
pytarget_object *target_obj = (pytarget_object *) self;
struct target_ops *ops = target_obj->ops;


PyObject *name;

const char *shortname;
Expand Down Expand Up @@ -789,8 +790,8 @@ tgt_py_get_name (PyObject *self, void * arg)
static int
tgt_py_set_name (PyObject *self, PyObject *newvalue, void * arg)
{
enum target_names target_string = (enum target_names) arg;
target_object *target_obj = (target_object *) self;
enum target_names target_string = (enum target_names) (intptr_t)arg;
pytarget_object *target_obj = (pytarget_object *) self;
struct target_ops *ops = target_obj->ops;
char *name = NULL;

Expand Down Expand Up @@ -845,7 +846,7 @@ static PyObject *target_getconst(PyObject *_self, void *_value)
#define CONST_GET(x) {#x, target_getconst, NULL, #x, (void*)x}


static PyGetSetDef target_object_getset[] =
static PyGetSetDef pytarget_object_getset[] =
{
{ "name", tgt_py_get_name, NULL,
"The name of the target", (void*)TGT_NAME },
Expand Down Expand Up @@ -909,7 +910,7 @@ CONST_GET(TARGET_OBJECT_EXEC_FILE),
static PyObject *
tgtpy_default_to_thread_name (PyObject *self, PyObject *args)
{
target_object *target_obj = (target_object *) self;
pytarget_object *target_obj = (pytarget_object *) self;
PyObject * ThreadName;

ENTRY();
Expand All @@ -921,7 +922,7 @@ tgtpy_default_to_thread_name (PyObject *self, PyObject *args)
return ThreadName;
}

static PyMethodDef target_object_methods[] =
static PyMethodDef pytarget_object_methods[] =
{
{ "to_thread_name_int", tgtpy_default_to_thread_name, METH_VARARGS | METH_KEYWORDS,
"to_thread_name (thread_info) -> String.\n\
Expand All @@ -936,8 +937,8 @@ Return string name representation of the given thread." },
static int
target_init (PyObject *self, PyObject *args, PyObject *kw)
{
target_object *target_obj = (target_object *) self;

pytarget_object *target_obj = (pytarget_object *) self;
struct target_ops *ops = target_obj->ops;
ENTRY();

TRY
Expand Down Expand Up @@ -971,9 +972,9 @@ gdbpy_initialize_target (void)
ENTRY();

/* Allow us to create instantiations of this class ... */
target_object_type.tp_new = PyType_GenericNew;
pytarget_object_type.tp_new = PyType_GenericNew;

if (PyType_Ready (&target_object_type) < 0)
if (PyType_Ready (&pytarget_object_type) < 0)
return -1;

py_target_xfer_eof_error = PyErr_NewException ("gdb.TargetXferEOF",
Expand All @@ -998,7 +999,7 @@ gdbpy_initialize_target (void)
EXIT ();

return gdb_pymodule_addobject (gdb_module, "Target",
(PyObject *) &target_object_type);
(PyObject *) &pytarget_object_type);
fail:
gdbpy_print_stack();
EXIT ();
Expand All @@ -1010,11 +1011,11 @@ gdbpy_initialize_target (void)



PyTypeObject target_object_type =
PyTypeObject pytarget_object_type =
{
PyVarObject_HEAD_INIT (NULL, 0)
"gdb.Target", /*tp_name*/
sizeof (target_object), /*tp_basicsize*/
sizeof (pytarget_object), /*tp_basicsize*/
0, /*tp_itemsize*/
target_dealloc, /*tp_dealloc*/
0, /*tp_print*/
Expand All @@ -1039,9 +1040,9 @@ PyTypeObject target_object_type =
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
target_object_methods, /* tp_methods */
pytarget_object_methods, /* tp_methods */
0, /* tp_members */
target_object_getset, /* tp_getset */
pytarget_object_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Expand All @@ -1055,9 +1056,9 @@ PyObject *
gdbpy_current_target (PyObject *self, PyObject *args)
{
struct target_ops *ops = current_target.beneath;
target_object *obj = target_ops_to_target_obj(ops);
pytarget_object *obj = target_ops_to_target_obj(ops);

if (obj->ops->to_data == &target_object_type)
if (obj->ops->to_data == &pytarget_object_type)
Py_INCREF(obj);

return (PyObject *)obj;
Expand Down
2 changes: 1 addition & 1 deletion gdb/python/py-target.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ typedef struct

struct target_ops python_ops;

} target_object;
} pytarget_object;

#endif /* GDB_PY_TARGET_H */