From b7e47ac1ac3f2d17536e8864db9c8bdada340b87 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 22 Aug 2013 12:10:13 +0200 Subject: pylibmount: add debug messages Signed-off-by: Karel Zak --- libmount/python/context.c | 2 ++ libmount/python/fs.c | 22 ++++++++++++++++---- libmount/python/pylibmount.c | 22 +++++++++++++++++++- libmount/python/pylibmount.h | 48 ++++++++++++++++++++++++++++++++++++++++++++ libmount/python/tab.c | 26 +++++++++++++++++++++++- 5 files changed, 114 insertions(+), 6 deletions(-) (limited to 'libmount/python') diff --git a/libmount/python/context.c b/libmount/python/context.c index 6cebc9f4c..9813efd37 100644 --- a/libmount/python/context.c +++ b/libmount/python/context.c @@ -1224,6 +1224,8 @@ void Context_AddModuleObject(PyObject *mod) if (PyType_Ready(&ContextType) < 0) return; + DBG(CXT, pymnt_debug("add to module")); + Py_INCREF(&ContextType); PyModule_AddObject(mod, "Context", (PyObject *)&ContextType); } diff --git a/libmount/python/fs.c b/libmount/python/fs.c index fd8024432..a1e8a1837 100644 --- a/libmount/python/fs.c +++ b/libmount/python/fs.c @@ -571,9 +571,10 @@ static PyMethodDef Fs_methods[] = { {NULL} }; -static void Fs_dealloc(FsObject *self) +static void Fs_destructor(FsObject *self) { - fprintf(stderr, "KZAK: [%p] delalocate\n", self->fs); + DBG(FS, pymnt_debug_h(self->fs, "destrutor py-obj: %p, py-refcnt=%d", + self, (int) ((PyObject *) self)->ob_refcnt)); mnt_unref_fs(self->fs); self->ob_type->tp_free((PyObject*)self); } @@ -583,8 +584,10 @@ static PyObject *Fs_new(PyTypeObject *type, PyObject *args __attribute__((unused { FsObject *self = (FsObject*)type->tp_alloc(type, 0); - if (self) + if (self) { self->fs = NULL; + DBG(FS, pymnt_debug_h(self, "new")); + } return (PyObject *) self; } @@ -606,6 +609,9 @@ static int Fs_init(FsObject *self, PyObject *args, PyObject *kwds) PyErr_SetString(PyExc_TypeError, "Invalid type"); return -1; } + + DBG(FS, pymnt_debug_h(self, "init")); + if (self->fs) mnt_unref_fs(self->fs); @@ -701,6 +707,8 @@ PyObject *PyObjectResultFs(struct libmnt_fs *fs) result = mnt_fs_get_userdata(fs); if (result) { Py_INCREF(result); + DBG(FS, pymnt_debug_h(fs, "result py-obj %p: already exists, py-refcnt=%d", + result, (int) ((PyObject *) result)->ob_refcnt)); return (PyObject *) result; } @@ -717,6 +725,8 @@ PyObject *PyObjectResultFs(struct libmnt_fs *fs) Py_INCREF(result); mnt_ref_fs(fs); + DBG(FS, pymnt_debug_h(fs, "result py-obj %p new, py-refcnt=%d", + result, (int) ((PyObject *) result)->ob_refcnt)); result->fs = fs; mnt_fs_set_userdata(fs, result); return (PyObject *) result; @@ -734,10 +744,13 @@ static PyObject *Fs_copy_fs(FsObject *self, PyObject *args, PyObject *kwds) if (PyObject_TypeCheck(dest, &FsType)) { /* existing object passed as argument */ if (!mnt_copy_fs(((FsObject *)dest)->fs, self->fs)) return NULL; + DBG(FS, pymnt_debug_h(dest, "copy data")); return (PyObject *)dest; } else if (dest == Py_None) { /* create new object */ FsObject *result = PyObject_New(FsObject, &FsType); + + DBG(FS, pymnt_debug_h(result, "new copy")); result->fs = mnt_copy_fs(NULL, self->fs); mnt_fs_set_userdata(result->fs, result); /* keep a pointer to encapsulating object */ return (PyObject *)result; @@ -754,7 +767,7 @@ PyTypeObject FsType = { "libmount.Fs", /*tp_name*/ sizeof(FsObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - (destructor)Fs_dealloc, /*tp_dealloc*/ + (destructor)Fs_destructor, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -795,6 +808,7 @@ void FS_AddModuleObject(PyObject *mod) if (PyType_Ready(&FsType) < 0) return; + DBG(FS, pymnt_debug("add to module")); Py_INCREF(&FsType); PyModule_AddObject(mod, "Fs", (PyObject *)&FsType); } diff --git a/libmount/python/pylibmount.c b/libmount/python/pylibmount.c index 5c9eca0e5..d2e79fbcc 100644 --- a/libmount/python/pylibmount.c +++ b/libmount/python/pylibmount.c @@ -22,6 +22,8 @@ /* Libmount-specific Exception class */ PyObject *LibmountError; +int pylibmount_debug_mask; + PyObject *UL_IncRef(void *killme) { @@ -130,9 +132,27 @@ PyMODINIT_FUNC initpylibmount(void) if (!m) return; + /* + * init debug stuff + */ + if (!(pylibmount_debug_mask & PYMNT_DEBUG_INIT)) { + char *str = getenv("PYLIBMOUNT_DEBUG"); + + pylibmount_debug_mask = 0; + if (str) + pylibmount_debug_mask = strtoul(str, 0, 0); - /*mnt_init_debug(0xffff);*/ + pylibmount_debug_mask |= PYMNT_DEBUG_INIT; + } + + if (pylibmount_debug_mask && pylibmount_debug_mask != PYMNT_DEBUG_INIT) + DBG(INIT, pymnt_debug("library debug mask: 0x%04x", + pylibmount_debug_mask)); + mnt_init_debug(0); + /* + * Add module objects + */ LibmountError = PyErr_NewException("libmount.Error", NULL, NULL); Py_INCREF(LibmountError); PyModule_AddObject(m, "Error", (PyObject *)LibmountError); diff --git a/libmount/python/pylibmount.h b/libmount/python/pylibmount.h index 28767b7ab..d3003af69 100644 --- a/libmount/python/pylibmount.h +++ b/libmount/python/pylibmount.h @@ -6,6 +6,54 @@ #include "libmount.h" +#define CONFIG_PYLIBMOUNT_DEBUG + +#define PYMNT_DEBUG_INIT (1 << 1) +#define PYMNT_DEBUG_TAB (1 << 2) +#define PYMNT_DEBUG_FS (1 << 3) +#define PYMNT_DEBUG_CXT (1 << 4) + +#ifdef CONFIG_PYLIBMOUNT_DEBUG +# include +# include + +# define DBG(m, x) do { \ + if ((PYMNT_DEBUG_ ## m) & pylibmount_debug_mask) { \ + fprintf(stderr, "%d: pylibmount: %6s: ", getpid(), # m); \ + x; \ + } \ + } while (0) + +extern int pylibmount_debug_mask; + +static inline void __attribute__ ((__format__ (__printf__, 1, 2))) +pymnt_debug(const char *mesg, ...) +{ + va_list ap; + va_start(ap, mesg); + vfprintf(stderr, mesg, ap); + va_end(ap); + fputc('\n', stderr); +} + +static inline void __attribute__ ((__format__ (__printf__, 2, 3))) +pymnt_debug_h(void *handler, const char *mesg, ...) +{ + va_list ap; + + if (handler) + fprintf(stderr, "[%p]: ", handler); + va_start(ap, mesg); + vfprintf(stderr, mesg, ap); + va_end(ap); + fputc('\n', stderr); +} + +#else /* !CONFIG_PYLIBMOUNT_DEBUG */ +# define DBG(m,x) do { ; } while (0) +#endif + + #define NODEL_ATTR "This attribute cannot be deleted" #define CONSTRUCT_ERR "Error during object construction" #define ARG_ERR "Invalid number or type of arguments" diff --git a/libmount/python/tab.c b/libmount/python/tab.c index 480203cb4..817643cf8 100644 --- a/libmount/python/tab.c +++ b/libmount/python/tab.c @@ -546,19 +546,27 @@ void Table_unref(struct libmnt_table *tab) if (!tab) return; + DBG(TAB, pymnt_debug_h(tab, "un-referencing filesystems")); + iter = mnt_new_iter(MNT_ITER_BACKWARD); /* remove pylibmount specific references to the entries */ while (mnt_table_next_fs(tab, iter, &fs) == 0) Py_XDECREF(mnt_fs_get_userdata(fs)); + DBG(TAB, pymnt_debug_h(tab, "un-referencing table")); + mnt_unref_table(tab); mnt_free_iter(iter); } static void Table_destructor(TableObject *self) { + DBG(TAB, pymnt_debug_h(self->tab, "destrutor py-obj: %p, py-refcnt=%d", + self, (int) ((PyObject *) self)->ob_refcnt)); Table_unref(self->tab); + self->tab = NULL; + mnt_free_iter(self->iter); Py_XDECREF(self->errcb); self->ob_type->tp_free((PyObject*)self); @@ -571,6 +579,8 @@ static PyObject *Table_new(PyTypeObject *type, TableObject *self = (TableObject*)type->tp_alloc(type, 0); if (self) { + DBG(TAB, pymnt_debug_h(self, "new")); + self->tab = NULL; self->iter = NULL; self->errcb = NULL; @@ -588,12 +598,15 @@ static int Table_init(TableObject *self, PyObject *args, PyObject *kwds) char *kwlist[] = {"path", "errcb", NULL}; PyObject *errcb = NULL; struct stat buf; + memset (&buf, 0, sizeof(struct stat)); if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sO", kwlist, &path, &errcb)) return -1; + DBG(TAB, pymnt_debug_h(self, "init")); + Table_unref(self->tab); self->tab = NULL; @@ -615,6 +628,8 @@ static int Table_init(TableObject *self, PyObject *args, PyObject *kwds) } if (path) { + DBG(TAB, pymnt_debug_h(self, "init: path defined (%s)", path)); + if (stat(path, &buf)) { /* TODO: weird */ PyErr_SetFromErrno(PyExc_RuntimeError); @@ -624,8 +639,10 @@ static int Table_init(TableObject *self, PyObject *args, PyObject *kwds) self->tab = mnt_new_table_from_file(path); else if (S_ISDIR(buf.st_mode)) self->tab = mnt_new_table_from_dir(path); - } else + } else { + DBG(TAB, pymnt_debug_h(self, "init: allocate empty table")); self->tab = mnt_new_table(); + } /* Always set custom handler when using libmount from python */ mnt_table_set_parser_errcb(self->tab, pymnt_table_parser_errcb); @@ -679,6 +696,8 @@ PyObject *PyObjectResultTab(struct libmnt_table *tab) result = mnt_table_get_userdata(tab); if (result) { Py_INCREF(result); + DBG(TAB, pymnt_debug_h(tab, "result py-obj %p: already exists, py-refcnt=%d", + result, (int) ((PyObject *) result)->ob_refcnt)); return (PyObject *) result; } @@ -694,6 +713,9 @@ PyObject *PyObjectResultTab(struct libmnt_table *tab) Py_INCREF(result); mnt_ref_table(tab); + + DBG(TAB, pymnt_debug_h(tab, "result py-obj %p new, py-refcnt=%d", + result, (int) ((PyObject *) result)->ob_refcnt)); result->tab = tab; result->iter = mnt_new_iter(MNT_ITER_FORWARD); mnt_table_set_userdata(result->tab, result); @@ -767,6 +789,8 @@ void Table_AddModuleObject(PyObject *mod) if (PyType_Ready(&TableType) < 0) return; + DBG(TAB, pymnt_debug("add to module")); + Py_INCREF(&TableType); PyModule_AddObject(mod, "Table", (PyObject *)&TableType); } -- cgit v1.2.3-55-g7522