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

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