Add $_memeq, $_regex, $_streq, $_strlen convenience functions.

* NEWS: Document them.
	* data-directory/Makefile.in (PYTHON_FILES): Add function/__init__.py,
	function/strfns.py.
	* python/py-type.c (typy_array_1): New function.
	(typy_array): Call it.
	(typy_vector): New function.
	(type_object_methods): Add "vector".
	* python/lib/gdb/function/__init__.py: New file.
	* python/lib/gdb/function/strfns.py: New file.

	doc/
	* gdb.texinfo (Convenience Funs): New node.
	(Types In Python): Document Type.vector.

	testsuite/
	* gdb.python/py-strfns.c: New file.
	* gdb.python/py-strfns.exp: New file.
	* gdb.python/py-type.exp (test_fields): Add vector tests.
This commit is contained in:
Doug Evans 2012-08-10 20:26:00 +00:00
parent 200bc880f4
commit a72c32530e
12 changed files with 405 additions and 3 deletions

View file

@ -0,0 +1,14 @@
# Copyright (C) 2012 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/>.

View file

@ -0,0 +1,108 @@
# Useful gdb string convenience functions.
# Copyright (C) 2012 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/>.
"""$_memeq, $_strlen, $_streq, $_regex"""
import gdb
import re
class _MemEq(gdb.Function):
"""$_memeq - compare bytes of memory
Usage:
$_memeq(a, b, len)
Returns:
True if len bytes at a and b compare equally.
"""
def __init__(self):
super(_MemEq, self).__init__("_memeq")
def invoke(self, a, b, length):
if length < 0:
raise ValueError("length must be non-negative")
if length == 0:
return True
# The argument(s) to vector are [low_bound,]high_bound.
byte_vector = gdb.lookup_type("char").vector(length - 1)
ptr_byte_vector = byte_vector.pointer()
a_ptr = a.reinterpret_cast(ptr_byte_vector)
b_ptr = b.reinterpret_cast(ptr_byte_vector)
return a_ptr.dereference() == b_ptr.dereference()
class _StrLen(gdb.Function):
"""$_strlen - compute string length
Usage:
$_strlen(a)
Returns:
Length of string a, assumed to be a string in the current language.
"""
def __init__(self):
super(_StrLen, self).__init__("_strlen")
def invoke(self, a):
s = a.string()
return len(s)
class _StrEq(gdb.Function):
"""$_streq - check string equality
Usage:
$_streq(a, b)
Returns:
True if a and b are identical strings in the current language.
Example (amd64-linux):
catch syscall open
cond $bpnum $_streq((char*) $rdi, "foo")
"""
def __init__(self):
super(_StrEq, self).__init__("_streq")
def invoke(self, a, b):
return a.string() == b.string()
class _RegEx(gdb.Function):
"""$_regex - check if a string matches a regular expression
Usage:
$_regex(string, regex)
Returns:
True if string str (in the current language) matches the
regular expression regex.
"""
def __init__(self):
super(_RegEx, self).__init__("_regex")
def invoke(self, string, regex):
s = string.string()
r = re.compile(regex.string())
return bool(r.match(s))
# GDB will import us automagically via gdb/__init__.py.
_MemEq()
_StrLen()
_StrEq()
_RegEx()

View file

@ -461,10 +461,10 @@ typy_get_composite (struct type *type)
return type;
}
/* Return an array type. */
/* Helper for typy_array and typy_vector. */
static PyObject *
typy_array (PyObject *self, PyObject *args)
typy_array_1 (PyObject *self, PyObject *args, int is_vector)
{
long n1, n2;
PyObject *n2_obj = NULL;
@ -503,12 +503,30 @@ typy_array (PyObject *self, PyObject *args)
TRY_CATCH (except, RETURN_MASK_ALL)
{
array = lookup_array_range_type (type, n1, n2);
if (is_vector)
make_vector_type (array);
}
GDB_PY_HANDLE_EXCEPTION (except);
return type_to_type_object (array);
}
/* Return an array type. */
static PyObject *
typy_array (PyObject *self, PyObject *args)
{
return typy_array_1 (self, args, 0);
}
/* Return a vector type. */
static PyObject *
typy_vector (PyObject *self, PyObject *args)
{
return typy_array_1 (self, args, 1);
}
/* Return a Type object which represents a pointer to SELF. */
static PyObject *
typy_pointer (PyObject *self, PyObject *args)
@ -1559,6 +1577,14 @@ static PyMethodDef type_object_methods[] =
Return a type which represents an array of objects of this type.\n\
The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
If LOW_BOUND is omitted, a value of zero is used." },
{ "vector", typy_vector, METH_VARARGS,
"vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\
Return a type which represents a vector of objects of this type.\n\
The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\
If LOW_BOUND is omitted, a value of zero is used.\n\
Vectors differ from arrays in that if the current language has C-style\n\
arrays, vectors don't decay to a pointer to the first element.\n\
They are first class values." },
{ "__contains__", typy_has_key, METH_VARARGS,
"T.__contains__(k) -> True if T has a field named k, else False" },
{ "const", typy_const, METH_NOARGS,