GDB (API)
/home/stan/gdb/linux/gdb/data-directory/python/gdb/function/strfns.py
Go to the documentation of this file.
00001 # Useful gdb string convenience functions.
00002 # Copyright (C) 2012-2013 Free Software Foundation, Inc.
00003 
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License as published by
00006 # the Free Software Foundation; either version 3 of the License, or
00007 # (at your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 """$_memeq, $_strlen, $_streq, $_regex"""
00018 
00019 import gdb
00020 import re
00021 
00022 
00023 class _MemEq(gdb.Function):
00024   """$_memeq - compare bytes of memory
00025 
00026 Usage:
00027   $_memeq(a, b, len)
00028 
00029 Returns:
00030   True if len bytes at a and b compare equally.
00031 """
00032   def __init__(self):
00033     super(_MemEq, self).__init__("_memeq")
00034 
00035   def invoke(self, a, b, length):
00036     if length < 0:
00037       raise ValueError("length must be non-negative")
00038     if length == 0:
00039       return True
00040     # The argument(s) to vector are [low_bound,]high_bound.
00041     byte_vector = gdb.lookup_type("char").vector(length - 1)
00042     ptr_byte_vector = byte_vector.pointer()
00043     a_ptr = a.reinterpret_cast(ptr_byte_vector)
00044     b_ptr = b.reinterpret_cast(ptr_byte_vector)
00045     return a_ptr.dereference() == b_ptr.dereference()
00046 
00047 
00048 class _StrLen(gdb.Function):
00049   """$_strlen - compute string length
00050 
00051 Usage:
00052   $_strlen(a)
00053 
00054 Returns:
00055   Length of string a, assumed to be a string in the current language.
00056 """
00057   def __init__(self):
00058     super(_StrLen, self).__init__("_strlen")
00059 
00060   def invoke(self, a):
00061     s = a.string()
00062     return len(s)
00063 
00064 
00065 class _StrEq(gdb.Function):
00066   """$_streq - check string equality
00067 
00068 Usage:
00069   $_streq(a, b)
00070 
00071 Returns:
00072   True if a and b are identical strings in the current language.
00073 
00074 Example (amd64-linux):
00075   catch syscall open
00076   cond $bpnum $_streq((char*) $rdi, "foo")
00077 """
00078   def __init__(self):
00079     super(_StrEq, self).__init__("_streq")
00080 
00081   def invoke(self, a, b):
00082     return a.string() == b.string()
00083 
00084 
00085 class _RegEx(gdb.Function):
00086   """$_regex - check if a string matches a regular expression
00087 
00088 Usage:
00089   $_regex(string, regex)
00090 
00091 Returns:
00092   True if string str (in the current language) matches the
00093   regular expression regex.
00094 """
00095   def __init__(self):
00096     super(_RegEx, self).__init__("_regex")
00097 
00098   def invoke(self, string, regex):
00099     s = string.string()
00100     r = re.compile(regex.string())
00101     return bool(r.match(s))
00102 
00103 
00104 # GDB will import us automagically via gdb/__init__.py.
00105 _MemEq()
00106 _StrLen()
00107 _StrEq()
00108 _RegEx()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines