Blame SOURCES/gdb-rhbz1007614-memleak-infpy_read_memory-test.patch

e1d87d
Original message by Tom Tromey:
e1d87d
e1d87d
  <https://sourceware.org/ml/gdb-patches/2012-03/msg00955.html>
e1d87d
  Message-ID: <871uoc1va3.fsf@fleche.redhat.com>
e1d87d
e1d87d
Comment from Sergio Durigan Junior:
e1d87d
e1d87d
  In order to correctly test this patch, I wrote a testcase based on Jan
e1d87d
  Kratochvil's <gdb/testsuite/gdb.base/gcore-excessive-memory.exp>.  The
e1d87d
  testcase, which can be seen below, tests GDB in order to see if the
e1d87d
  amount of memory being leaked is minimal, as requested in the bugzilla.
e1d87d
  It is hard to define what "minimum" is, so I ran the testcase on all
e1d87d
  supported RHEL architectures and came up with an average.
e1d87d
e1d87d
commit cc0265cdda9dc7e8665e8bfcf5b4477489daf27c
e1d87d
Author: Tom Tromey <tromey@redhat.com>
e1d87d
Date:   Wed Mar 28 17:38:08 2012 +0000
e1d87d
e1d87d
    	* python/py-inferior.c (infpy_read_memory): Remove cleanups and
e1d87d
    	explicitly free 'buffer' on exit paths.  Decref 'membuf_object'
e1d87d
    	before returning.
e1d87d
e1d87d
Index: gdb-7.2/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.c
e1d87d
===================================================================
e1d87d
--- /dev/null
e1d87d
+++ gdb-7.2/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.c
e1d87d
@@ -0,0 +1,27 @@
e1d87d
+/* This testcase is part of GDB, the GNU debugger.
e1d87d
+
e1d87d
+   Copyright 2014 Free Software Foundation, Inc.
e1d87d
+
e1d87d
+   This program is free software; you can redistribute it and/or modify
e1d87d
+   it under the terms of the GNU General Public License as published by
e1d87d
+   the Free Software Foundation; either version 3 of the License, or
e1d87d
+   (at your option) any later version.
e1d87d
+
e1d87d
+   This program is distributed in the hope that it will be useful,
e1d87d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e1d87d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e1d87d
+   GNU General Public License for more details.
e1d87d
+
e1d87d
+   You should have received a copy of the GNU General Public License
e1d87d
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
e1d87d
+
e1d87d
+static struct x
e1d87d
+  {
e1d87d
+    char unsigned u[4096];
e1d87d
+  } x, *px = &x;
e1d87d
+
e1d87d
+int
e1d87d
+main (int argc, char *argv[])
e1d87d
+{
e1d87d
+  return 0;
e1d87d
+}
e1d87d
Index: gdb-7.2/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.exp
e1d87d
===================================================================
e1d87d
--- /dev/null
e1d87d
+++ gdb-7.2/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.exp
e1d87d
@@ -0,0 +1,68 @@
e1d87d
+# Copyright 2014 Free Software Foundation, Inc.
e1d87d
+
e1d87d
+# This program is free software; you can redistribute it and/or modify
e1d87d
+# it under the terms of the GNU General Public License as published by
e1d87d
+# the Free Software Foundation; either version 3 of the License, or
e1d87d
+# (at your option) any later version.
e1d87d
+#
e1d87d
+# This program is distributed in the hope that it will be useful,
e1d87d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
e1d87d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e1d87d
+# GNU General Public License for more details.
e1d87d
+#
e1d87d
+# You should have received a copy of the GNU General Public License
e1d87d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
e1d87d
+
e1d87d
+set testfile py-gdb-rhbz1007614-memleak-infpy_read_memory
e1d87d
+set srcfile ${testfile}.c
e1d87d
+set binfile [standard_output_file ${testfile}]
e1d87d
+
e1d87d
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
e1d87d
+    return -1
e1d87d
+}
e1d87d
+
e1d87d
+if { [skip_python_tests] } { continue }
e1d87d
+
e1d87d
+set pid_of_gdb [exp_pid -i [board_info host fileid]]
e1d87d
+
e1d87d
+proc memory_v_pages_get {} {
e1d87d
+    global pid_of_gdb
e1d87d
+    set fd [open "/proc/$pid_of_gdb/statm"]
e1d87d
+    gets $fd line
e1d87d
+    close $fd
e1d87d
+    # number of pages of virtual memory
e1d87d
+    scan $line "%d" drs
e1d87d
+    return $drs
e1d87d
+}
e1d87d
+
e1d87d
+if { ![runto_main] } {
e1d87d
+    untested $testfile.exp
e1d87d
+    return -1
e1d87d
+}
e1d87d
+
e1d87d
+set remote_python_file [remote_download host ${srcdir}/${subdir}/${testfile}.py]
e1d87d
+
e1d87d
+gdb_test "source ${remote_python_file}" ""
e1d87d
+
e1d87d
+gdb_test "hello-world" ""
e1d87d
+
e1d87d
+set kbytes_before [memory_v_pages_get]
e1d87d
+verbose -log "kbytes_before = $kbytes_before"
e1d87d
+
e1d87d
+gdb_test "hello-world" ""
e1d87d
+
e1d87d
+set kbytes_after [memory_v_pages_get]
e1d87d
+verbose -log "kbytes_after = $kbytes_after"
e1d87d
+
e1d87d
+set kbytes_diff [expr $kbytes_after - $kbytes_before]
e1d87d
+verbose -log "kbytes_diff = $kbytes_diff"
e1d87d
+
e1d87d
+# The value "1000" was calculated by running a few GDB sessions with this
e1d87d
+# testcase, and seeing how much (in average) the memory consumption
e1d87d
+# increased after the "hello-world" command issued above.  The average
e1d87d
+# was around 500 bytes, so I chose 1000 as a high estimate.
e1d87d
+if { $kbytes_diff > 1000 } {
e1d87d
+    fail "there is a memory leak on GDB (RHBZ 1007614)"
e1d87d
+} else {
e1d87d
+    pass "there is not a memory leak on GDB (RHBZ 1007614)"
e1d87d
+}
e1d87d
Index: gdb-7.2/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.py
e1d87d
===================================================================
e1d87d
--- /dev/null
e1d87d
+++ gdb-7.2/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.py
e1d87d
@@ -0,0 +1,30 @@
e1d87d
+# Copyright (C) 2014 Free Software Foundation, Inc.
e1d87d
+
e1d87d
+# This program is free software; you can redistribute it and/or modify
e1d87d
+# it under the terms of the GNU General Public License as published by
e1d87d
+# the Free Software Foundation; either version 3 of the License, or
e1d87d
+# (at your option) any later version.
e1d87d
+#
e1d87d
+# This program is distributed in the hope that it will be useful,
e1d87d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
e1d87d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e1d87d
+# GNU General Public License for more details.
e1d87d
+#
e1d87d
+# You should have received a copy of the GNU General Public License
e1d87d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
e1d87d
+
e1d87d
+class HelloWorld (gdb.Command):
e1d87d
+    """Greet the whole world."""
e1d87d
+
e1d87d
+    def __init__ (self):
e1d87d
+        super (HelloWorld, self).__init__ ("hello-world",
e1d87d
+                                           gdb.COMMAND_OBSCURE)
e1d87d
+
e1d87d
+    def invoke (self, arg, from_tty):
e1d87d
+        px = gdb.parse_and_eval("px")
e1d87d
+        core = gdb.inferiors()[0]
e1d87d
+        for i in range(256 * 1024):
e1d87d
+            chunk = core.read_memory(px, 4096)
e1d87d
+        print "Hello, World!"
e1d87d
+
e1d87d
+HelloWorld ()