public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug python/10896] New: gdb python hooks leak struct value *s
@ 2009-11-03  3:25 gdb at tmm1 dot net
  2009-12-10 20:45 ` [Bug python/10896] " pmuldoon at redhat dot com
  2010-06-30  9:06 ` pmuldoon at redhat dot com
  0 siblings, 2 replies; 5+ messages in thread
From: gdb at tmm1 dot net @ 2009-11-03  3:25 UTC (permalink / raw)
  To: gdb-prs

$ cat simple.c 
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

struct blah {
  char test[2];
  int i;
  float none;
} *list_of_blahs;

int main(){
  list_of_blahs = malloc(sizeof(struct blah) * 2000000);
  printf("pid: %d\n", getpid());
  while(1){
    sleep(1);
  }
}


$ cat simple.py 
gdb.execute('p list_of_blahs')
list = gdb.history(0)
end = list + 1000000

while list < end:
  list['i']
  list += 1

gdb will leak a large amount of memory while executing the python loop. Some (but not all) of this 
memory is returned after the loop completes and free_all_values() is called for the next command. The 
following patch keeps the memory usage constant during the loop:

diff --git a/python/py-value.c b/python/py-value.c
index 8f8c610..c3a6cbf 100644
--- a/python/py-value.c
+++ b/python/py-value.c
@@ -139,7 +139,7 @@ valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
     }
 
   value_obj->value = value;
-  value_incref (value);
+  release_value_or_incref (value);
   value_obj->address = NULL;
   value_obj->type = NULL;
   note_value (value_obj);
@@ -263,6 +263,7 @@ valpy_cast (PyObject *self, PyObject *args)
 {
   PyObject *type_obj;
   struct type *type;
+  PyObject *res_obj = NULL;
   struct value *res_val = NULL;	  /* Initialize to appease gcc warning.  */
   volatile struct gdb_exception except;
 
@@ -301,8 +302,10 @@ valpy_getitem (PyObject *self, PyObject *key)
 {
   value_object *self_value = (value_object *) self;
   char *field = NULL;
+  PyObject *res_obj = NULL;
   struct value *res_val = NULL;
   volatile struct gdb_exception except;
+  struct value *mark = value_mark ();
 
   if (gdbpy_is_string (key))
     {  
@@ -340,9 +343,13 @@ valpy_getitem (PyObject *self, PyObject *key)
     }
 
   xfree (field);
-  GDB_PY_HANDLE_EXCEPTION (except);
+  GDB_PY_HANDLE_EXCEPTION_AND_CLEANUP (except, mark);
+
+  if (res_val)
+    res_obj = value_to_value_object (res_val);
 
-  return res_val ? value_to_value_object (res_val) : NULL;
+  value_free_to_mark (mark);
+  return res_obj;
 }
 
 static int
@@ -423,8 +430,10 @@ enum valpy_opcode
 static PyObject *
 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
 {
+  PyObject *res_obj = NULL;
   struct value *res_val = NULL;	  /* Initialize to appease gcc warning.  */
   volatile struct gdb_exception except;
+  struct value *mark = value_mark ();
 
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
@@ -515,9 +524,13 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
 	  break;
 	}
     }
-  GDB_PY_HANDLE_EXCEPTION (except);
+  GDB_PY_HANDLE_EXCEPTION_AND_CLEANUP (except, mark);
+
+  if (res_val)
+    res_obj = value_to_value_object (res_val);
 
-  return res_val ? value_to_value_object (res_val) : NULL;
+  value_free_to_mark (mark);
+  return res_obj;
 }
 
 static PyObject *
@@ -680,6 +693,7 @@ valpy_richcompare (PyObject *self, PyObject *other, int op)
   int result = 0;
   struct value *value_other;
   volatile struct gdb_exception except;
+  struct value *mark = value_mark ();
 
   if (other == Py_None)
     /* Comparing with None is special.  From what I can tell, in Python
@@ -738,6 +752,7 @@ valpy_richcompare (PyObject *self, PyObject *other, int op)
 	  break;
       }
     }
+  value_free_to_mark (mark);
   GDB_PY_HANDLE_EXCEPTION (except);
 
   /* In this case, the Python exception has already been set.  */
@@ -858,7 +873,7 @@ value_to_value_object (struct value *val)
   if (val_obj != NULL)
     {
       val_obj->value = val;
-      value_incref (val);
+      release_value_or_incref (val);
       val_obj->address = NULL;
       val_obj->type = NULL;
       note_value (val_obj);
diff --git a/python/python-internal.h b/python/python-internal.h
index fa4a62b..bc9c8c0 100644
--- a/python/python-internal.h
+++ b/python/python-internal.h
@@ -103,11 +103,17 @@ extern const struct language_defn *python_language;
 /* Use this after a TRY_EXCEPT to throw the appropriate Python
    exception.  */
 #define GDB_PY_HANDLE_EXCEPTION(Exception)				\
+    GDB_PY_HANDLE_EXCEPTION_AND_CLEANUP(Exception, NULL)
+
+#define GDB_PY_HANDLE_EXCEPTION_AND_CLEANUP(Exception, mark)				\
     do {								\
-      if (Exception.reason < 0)						\
+      if (Exception.reason < 0) {					\
+        if (mark)						\
+          value_free_to_mark (mark);						\
 	return PyErr_Format (Exception.reason == RETURN_QUIT		\
 			     ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, \
 			     "%s", Exception.message);			\
+      }						\
     } while (0)
 
 
diff --git a/value.c b/value.c
index 589e03b..59a0196 100644
--- a/value.c
+++ b/value.c
@@ -675,7 +675,7 @@ free_all_values (void)
 /* Remove VAL from the chain all_values
    so it will not be freed automatically.  */
 
-void
+int
 release_value (struct value *val)
 {
   struct value *v;
@@ -683,7 +683,7 @@ release_value (struct value *val)
   if (all_values == val)
     {
       all_values = val->next;
-      return;
+      return 1;
     }
 
   for (v = all_values; v; v = v->next)
@@ -691,9 +691,21 @@ release_value (struct value *val)
       if (v->next == val)
 	{
 	  v->next = val->next;
-	  break;
+	  return 1;
 	}
     }
+
+  return 0;
+}
+
+/* Release VAL or increment its reference count if
+   it was released already */
+
+void
+release_value_or_incref (struct value *val)
+{
+  if (release_value (val) == 0)
+    value_incref (val);
 }
 
 /* Release all values up to mark  */
diff --git a/value.h b/value.h
index 51e6960..4760c4a 100644
--- a/value.h
+++ b/value.h
@@ -594,7 +594,9 @@ extern void value_free (struct value *val);
 
 extern void free_all_values (void);
 
-extern void release_value (struct value *val);
+extern int release_value (struct value *val);
+
+extern void release_value_or_incref (struct value *val);
 
 extern int record_latest_value (struct value *val);

-- 
           Summary: gdb python hooks leak struct value *s
           Product: gdb
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: python
        AssignedTo: unassigned at sourceware dot org
        ReportedBy: gdb at tmm1 dot net
                CC: gdb-prs at sourceware dot org


http://sourceware.org/bugzilla/show_bug.cgi?id=10896

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug python/10896] gdb python hooks leak struct value *s
  2009-11-03  3:25 [Bug python/10896] New: gdb python hooks leak struct value *s gdb at tmm1 dot net
@ 2009-12-10 20:45 ` pmuldoon at redhat dot com
  2010-06-30  9:06 ` pmuldoon at redhat dot com
  1 sibling, 0 replies; 5+ messages in thread
From: pmuldoon at redhat dot com @ 2009-12-10 20:45 UTC (permalink / raw)
  To: gdb-prs


------- Additional Comments From pmuldoon at redhat dot com  2009-12-10 20:45 -------
Sorry for the very long delay.  I talked with Tom today and I said I would look
at this patch.

Can you extract this patch against the current sources, and post to the archer
mailing list? That way we can review/change/review the changes to the list.

Also, if this patch is approved, I believe you will need papers for assignment
with the FSF. Do you have them? 

-- 


http://sourceware.org/bugzilla/show_bug.cgi?id=10896

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug python/10896] gdb python hooks leak struct value *s
  2009-11-03  3:25 [Bug python/10896] New: gdb python hooks leak struct value *s gdb at tmm1 dot net
  2009-12-10 20:45 ` [Bug python/10896] " pmuldoon at redhat dot com
@ 2010-06-30  9:06 ` pmuldoon at redhat dot com
  1 sibling, 0 replies; 5+ messages in thread
From: pmuldoon at redhat dot com @ 2010-06-30  9:06 UTC (permalink / raw)
  To: gdb-prs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING


http://sourceware.org/bugzilla/show_bug.cgi?id=10896

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug python/10896] gdb python hooks leak struct value *s
       [not found] <bug-10896-4717@http.sourceware.org/bugzilla/>
  2012-12-18 15:12 ` tromey at redhat dot com
@ 2014-09-12 22:17 ` sergiodj at redhat dot com
  1 sibling, 0 replies; 5+ messages in thread
From: sergiodj at redhat dot com @ 2014-09-12 22:17 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=10896

Sergio Durigan Junior <sergiodj at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sergiodj at redhat dot com

--- Comment #3 from Sergio Durigan Junior <sergiodj at redhat dot com> ---
Hi Aman and Phil, what is the status of this issue?  Aman, have you been able
to try a recent GDB as Tom requested?

-- 
You are receiving this mail because:
You are on the CC list for the bug.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug python/10896] gdb python hooks leak struct value *s
       [not found] <bug-10896-4717@http.sourceware.org/bugzilla/>
@ 2012-12-18 15:12 ` tromey at redhat dot com
  2014-09-12 22:17 ` sergiodj at redhat dot com
  1 sibling, 0 replies; 5+ messages in thread
From: tromey at redhat dot com @ 2012-12-18 15:12 UTC (permalink / raw)
  To: gdb-prs

http://sourceware.org/bugzilla/show_bug.cgi?id=10896

Tom Tromey <tromey at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tromey at redhat dot com

--- Comment #2 from Tom Tromey <tromey at redhat dot com> 2012-12-18 15:12:33 UTC ---
I think the leak here was fixed a while ago.
Can you try again and see?

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-09-12 22:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-03  3:25 [Bug python/10896] New: gdb python hooks leak struct value *s gdb at tmm1 dot net
2009-12-10 20:45 ` [Bug python/10896] " pmuldoon at redhat dot com
2010-06-30  9:06 ` pmuldoon at redhat dot com
     [not found] <bug-10896-4717@http.sourceware.org/bugzilla/>
2012-12-18 15:12 ` tromey at redhat dot com
2014-09-12 22:17 ` sergiodj at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).