]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
pylibmount: add debug messages
authorKarel Zak <kzak@redhat.com>
Thu, 22 Aug 2013 10:10:13 +0000 (12:10 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 22 Aug 2013 10:10:13 +0000 (12:10 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/python/context.c
libmount/python/fs.c
libmount/python/pylibmount.c
libmount/python/pylibmount.h
libmount/python/tab.c

index 6cebc9f4ce9f5479226c5feb07e190957b9f68f5..9813efd372128ab452c4ca0d2c160340d4d6a4f2 100644 (file)
@@ -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);
 }
index fd8024432d687f54c5092157d2bf07edc6fffbad..a1e8a1837b32d8b094491691172e18a707f67efe 100644 (file)
@@ -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);
 }
index 5c9eca0e59f6bd6de3987b5d88fb184bb24b27b3..d2e79fbccfcdf8569fefe666019863d32a239d96 100644 (file)
@@ -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);
index 28767b7ab8eaeec844e4d0a32b26efe0e7a9b9b1..d3003af69de5a69e776396fa96434e1cbefd017a 100644 (file)
@@ -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 <stdio.h>
+# include <stdarg.h>
+
+# 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"
index 480203cb4dee49646b639ce24b8817a910c5f279..817643cf80ae51c52d76fe2e474a34850bd610ed 100644 (file)
@@ -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);
 }