diff mbox

[libstdc++] Add xmethods for std::vector and std::unique_ptr

Message ID CAGyQ6gxpgbnNobkdKeOD5jGQRbo7KG4wbe5XXyf_eMvNW1k9Aw@mail.gmail.com
State New
Headers show

Commit Message

Siva Chandra June 29, 2014, 3:08 p.m. UTC
GDB now supports xmethods in its Python API:
https://sourceware.org/gdb/current/onlinedocs/gdb/Xmethods-In-Python.html

This feature will be available in GDB starting version 7.8 (which has
not yet been released, but has been branched). The attached patch adds
xmethods to the classes std::vector and std::unique_ptr. One can of
course add xmethods to many other classes, but I am viewing this as
the first patch in that series (though not a series yet) to get the
basic infrastructure for adding more xmethods in place.

ChangeLog

libstdc++-v3/

2014-06-29  Siva Chandra Reddy  <sivachandra@google.com>

        * python/hook.in: Register libstdcxx xmethods.
        * python/libstdcxx/v6/xmethods.py: New file.
        * testsuite/lib/gdb-test.exp (gdb_version_check_xmethods): New
        function.
        (gdb-test): New optional argument LOAD_XMETHODS.  Load xmethods
        python script if LOAD_XMETHODS is true.
        * testsuite/libstdc++-xmethods/unique_ptr.cc: New file.
        * testsuite/libstdc++-xmethods/vector.cc: New file.
        * testsuite/libstdc++-xmethods/xmethods.exp: New file.

Comments

Tom Tromey June 30, 2014, 3 p.m. UTC | #1
>>>>> "Siva" == Siva Chandra <sivachandra@google.com> writes:

Siva> +# Load the xmethods.
Siva> +from libstdcxx.v6.xmethods import register_libstdcxx_xmethods
Siva> +register_libstdcxx_xmethods (gdb.current_objfile ())

I don't think any addition to the hook file should be needed.

Siva> +        # The object to be returned is the 0-th element in the tuple _m_t.
Siva> +        # The following retrieves the 0-th element of this tuple.
Siva> +        _m_t_base = _m_t[_m_t.type.fields()[0]] # std::tuple has a single base
Siva> +                                                # class and no data members.
Siva> +        for f in _m_t_base.type.fields():
Siva> +            # The object is embedded in the _Head_base<> base class of
Siva> +            # _m_t_base.
Siva> +            if f.is_base_class and f.name.find('std::_Head_base<') == 0:
Siva> +                _head_base = _m_t_base[f]

Did you investigate sharing any code with the printers?
If so, why did you choose not to?
If not, could you please look into that?

Tom
diff mbox

Patch

diff --git a/libstdc++-v3/python/hook.in b/libstdc++-v3/python/hook.in
index 3620523..34368b5 100644
--- a/libstdc++-v3/python/hook.in
+++ b/libstdc++-v3/python/hook.in
@@ -58,3 +58,7 @@  if gdb.current_objfile () is not None:
 # Load the pretty-printers.
 from libstdcxx.v6.printers import register_libstdcxx_printers
 register_libstdcxx_printers (gdb.current_objfile ())
+
+# Load the xmethods.
+from libstdcxx.v6.xmethods import register_libstdcxx_xmethods
+register_libstdcxx_xmethods (gdb.current_objfile ())
diff --git a/libstdc++-v3/python/libstdcxx/v6/xmethods.py b/libstdc++-v3/python/libstdcxx/v6/xmethods.py
new file mode 100644
index 0000000..1b4b721
--- /dev/null
+++ b/libstdc++-v3/python/libstdcxx/v6/xmethods.py
@@ -0,0 +1,113 @@ 
+# Xmethods for libstc++.
+
+# Copyright (C) 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+import gdb.xmethod
+import re
+
+matcher_name_prefix = 'libstdc++::'
+
+# Xmethods for std::vector
+
+class VectorSizeWorker(gdb.xmethod.XMethodWorker):
+    def __init__(self):
+        self.name = 'size'
+        self.enabled = True
+
+    def get_arg_types(self):
+        return None
+
+    def __call__(self, obj):
+        return obj['_M_impl']['_M_finish'] - obj['_M_impl']['_M_start']
+
+class VectorSubscriptWorker(gdb.xmethod.XMethodWorker):
+    def __init__(self):
+        self.name = 'operator[]'
+        self.enabled = True
+
+    def get_arg_types(self):
+        return gdb.lookup_type('std::size_t')
+
+    def __call__(self, obj, subscript):
+        return obj['_M_impl']['_M_start'][subscript]
+
+class VectorMethodsMatcher(gdb.xmethod.XMethodMatcher):
+    def __init__(self):
+        gdb.xmethod.XMethodMatcher.__init__(self,
+                                            matcher_name_prefix + 'vector')
+        self._subscript_worker = VectorSubscriptWorker()
+        self._size_worker = VectorSizeWorker()
+        self.methods = [self._subscript_worker, self._size_worker]
+
+    def match(self, class_type, method_name):
+        if not re.match('^std::vector<.*>$', class_type.tag):
+            return None
+        if method_name == 'operator[]' and self._subscript_worker.enabled:
+            return self._subscript_worker
+        elif method_name == 'size' and self._size_worker.enabled:
+            return self._size_worker
+
+# Xmethods for std::unique_ptr
+
+class UniquePtrGetWorker(gdb.xmethod.XMethodWorker):
+    def __init__(self):
+        self.name = 'get'
+        self.enabled = True
+
+    def get_arg_types(self):
+        return None
+
+    def __call__(self, obj):
+        _m_t = obj['_M_t']
+        # The object to be returned is the 0-th element in the tuple _m_t.
+        # The following retrieves the 0-th element of this tuple.
+        _m_t_base = _m_t[_m_t.type.fields()[0]] # std::tuple has a single base
+                                                # class and no data members.
+        for f in _m_t_base.type.fields():
+            # The object is embedded in the _Head_base<> base class of
+            # _m_t_base.
+            if f.is_base_class and f.name.find('std::_Head_base<') == 0:
+                _head_base = _m_t_base[f]
+        return _head_base['_M_head_impl']
+
+class UniquePtrDerefWorker(UniquePtrGetWorker):
+    def __init__(self):
+        UniquePtrGetWorker.__init__(self)
+        self.name = 'operator*'
+
+    def __call__(self, obj):
+        return UniquePtrGetWorker.__call__(self, obj).dereference()
+
+class UniquePtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
+    def __init__(self):
+        gdb.xmethod.XMethodMatcher.__init__(self,
+                                            matcher_name_prefix + 'unique_ptr')
+        self._get_worker = UniquePtrGetWorker()
+        self._deref_worker = UniquePtrDerefWorker()
+        self.methods = [self._get_worker, self._deref_worker]
+
+    def match(self, class_type, method_name):
+        if not re.match('^std::unique_ptr<.*>$', class_type.tag):
+            return None
+        if method_name == 'operator*' and self._deref_worker.enabled:
+            return self._deref_worker
+        elif method_name == 'get' and self._get_worker.enabled:
+            return self._get_worker
+
+def register_libstdcxx_xmethods(locus):
+    gdb.xmethod.register_xmethod_matcher(locus, VectorMethodsMatcher())
+    gdb.xmethod.register_xmethod_matcher(locus, UniquePtrMethodsMatcher())
diff --git a/libstdc++-v3/testsuite/lib/gdb-test.exp b/libstdc++-v3/testsuite/lib/gdb-test.exp
index 1a72a44..adc788f 100644
--- a/libstdc++-v3/testsuite/lib/gdb-test.exp
+++ b/libstdc++-v3/testsuite/lib/gdb-test.exp
@@ -79,7 +79,7 @@  proc whatis-test {var result} {
 #
 # Argument 0 is the marker on which to put a breakpoint
 # Argument 2 handles expected failures and the like
-proc gdb-test { marker {selector {}} } {
+proc gdb-test { marker {selector {}} {load_xmethods 0} } {
     if { ![isnative] || [is_remote target] } { return }
 
     if {[string length $selector] > 0} {
@@ -111,13 +111,18 @@  proc gdb-test { marker {selector {}} } {
     set cmd_file "[file rootname [file tail $prog]].gdb"
 
     global srcdir
-    set pycode [file join $srcdir .. python libstdcxx v6 printers.py]
+    set printer_code [file join $srcdir .. python libstdcxx v6 printers.py]
+    set xmethod_code [file join $srcdir .. python libstdcxx v6 xmethods.py]
 
     global gdb_tests
 
     set fd [open $cmd_file "w"]
-    puts $fd "source $pycode"
+    puts $fd "source $printer_code"
     puts $fd "python register_libstdcxx_printers(None)"
+    if { $load_xmethods } {
+	puts $fd "source $xmethod_code"
+	puts $fd "python register_libstdcxx_xmethods(None)"
+    }
     puts $fd "break $line"
     puts $fd "run"
 
@@ -255,3 +260,12 @@  proc gdb_version_check {} {
     return [gdb_batch_check "python print gdb.lookup_global_symbol" \
 	      "<built-in function lookup_global_symbol>"]
 }
+
+# Check for a version of gdb which supports xmethod tests.  It is done
+# in a manner similar to the check for a version of gdb which supports the
+# pretty-printer tests below.
+proc gdb_version_check_xmethods {} {
+    return [gdb_batch_check \
+	      "python import gdb.xmethod; print gdb.xmethod.XMethod" \
+	      "<class 'gdb\\.xmethod\\.XMethod'>"]
+}
diff --git a/libstdc++-v3/testsuite/libstdc++-xmethods/unique_ptr.cc b/libstdc++-v3/testsuite/libstdc++-xmethods/unique_ptr.cc
new file mode 100644
index 0000000..6c9fd8e
--- /dev/null
+++ b/libstdc++-v3/testsuite/libstdc++-xmethods/unique_ptr.cc
@@ -0,0 +1,36 @@ 
+// { dg-do run }
+// { dg-options "-std=gnu++11 -g -O0" }
+
+// Copyright (C) 2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <memory>
+
+int
+main ()
+{
+  int *i = new int;
+  *i = 10;
+
+  std::unique_ptr<int> p(i);
+// { dg-final { note-test *p 10 } }
+// { dg-final { regexp-test p.get() 0x.* } }
+
+  return 0;  // Mark SPOT
+}
+
+// { dg-final { gdb-test SPOT {} 1 } }
diff --git a/libstdc++-v3/testsuite/libstdc++-xmethods/vector.cc b/libstdc++-v3/testsuite/libstdc++-xmethods/vector.cc
new file mode 100644
index 0000000..2ee9399
--- /dev/null
+++ b/libstdc++-v3/testsuite/libstdc++-xmethods/vector.cc
@@ -0,0 +1,38 @@ 
+// { dg-do run }
+// { dg-options "-g -O0" }
+
+// Copyright (C) 2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <vector>
+
+int
+main ()
+{
+  std::vector<int> v;
+  v.push_back(1);
+  v.push_back(2);
+  v.push_back(3);
+// { dg-final { note-test v\[0\] 1 } }
+// { dg-final { note-test v\[1\] 2 } }
+// { dg-final { note-test v\[2\] 3 } }
+// { dg-final { note-test v.size() 3 } }
+
+  return 0;  // Mark SPOT
+}
+
+// { dg-final { gdb-test SPOT {} 1 } }
diff --git a/libstdc++-v3/testsuite/libstdc++-xmethods/xmethods.exp b/libstdc++-v3/testsuite/libstdc++-xmethods/xmethods.exp
new file mode 100644
index 0000000..c51c7e8
--- /dev/null
+++ b/libstdc++-v3/testsuite/libstdc++-xmethods/xmethods.exp
@@ -0,0 +1,51 @@ 
+#   Copyright (C) 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+load_lib gdb-test.exp
+
+dg-init
+v3-build_support
+
+global GDB
+if ![info exists ::env(GUALITY_GDB_NAME)] {
+    if [info exists GDB] {
+	set guality_gdb_name "$GDB"
+    } else {
+	set guality_gdb_name "[transform gdb]"
+    }
+    setenv GUALITY_GDB_NAME "$guality_gdb_name"
+}
+
+if {! [gdb_version_check_xmethods]} {
+    unsupported "xmethods.exp"
+    return
+}
+
+# This can be used to keep the .exe around.  dg-test has an option for
+# this but there is no way to pass it through dg-runtest.
+global dg-interpreter-batch-mode
+set dg-interpreter-batch-mode 1
+
+global DEFAULT_CXXFLAGS
+global PCH_CXXFLAGS
+dg-runtest [lsort [glob $srcdir/$subdir/*.cc]] \
+  "" "$DEFAULT_CXXFLAGS $PCH_CXXFLAGS"
+
+if [info exists guality_gdb_name] {
+    unsetenv GUALITY_GDB_NAME
+}
+
+dg-finish