blob: 9e2ed79a449ab8d61367cbab0c66738d876a1c19 [file] [log] [blame]
Ben Cheng09ebc132013-10-18 00:02:49 -07001# Useful gdb string convenience functions.
Ben Cheng7334f0a2014-04-30 14:28:17 -07002# Copyright (C) 2012-2014 Free Software Foundation, Inc.
Ben Cheng09ebc132013-10-18 00:02:49 -07003
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17"""$_memeq, $_strlen, $_streq, $_regex"""
18
19import gdb
20import re
21
22
23class _MemEq(gdb.Function):
24 """$_memeq - compare bytes of memory
25
26Usage:
27 $_memeq(a, b, len)
28
29Returns:
30 True if len bytes at a and b compare equally.
31"""
32 def __init__(self):
33 super(_MemEq, self).__init__("_memeq")
34
35 def invoke(self, a, b, length):
36 if length < 0:
37 raise ValueError("length must be non-negative")
38 if length == 0:
39 return True
40 # The argument(s) to vector are [low_bound,]high_bound.
41 byte_vector = gdb.lookup_type("char").vector(length - 1)
42 ptr_byte_vector = byte_vector.pointer()
43 a_ptr = a.reinterpret_cast(ptr_byte_vector)
44 b_ptr = b.reinterpret_cast(ptr_byte_vector)
45 return a_ptr.dereference() == b_ptr.dereference()
46
47
48class _StrLen(gdb.Function):
49 """$_strlen - compute string length
50
51Usage:
52 $_strlen(a)
53
54Returns:
55 Length of string a, assumed to be a string in the current language.
56"""
57 def __init__(self):
58 super(_StrLen, self).__init__("_strlen")
59
60 def invoke(self, a):
61 s = a.string()
62 return len(s)
63
64
65class _StrEq(gdb.Function):
66 """$_streq - check string equality
67
68Usage:
69 $_streq(a, b)
70
71Returns:
72 True if a and b are identical strings in the current language.
73
74Example (amd64-linux):
75 catch syscall open
76 cond $bpnum $_streq((char*) $rdi, "foo")
77"""
78 def __init__(self):
79 super(_StrEq, self).__init__("_streq")
80
81 def invoke(self, a, b):
82 return a.string() == b.string()
83
84
85class _RegEx(gdb.Function):
86 """$_regex - check if a string matches a regular expression
87
88Usage:
89 $_regex(string, regex)
90
91Returns:
92 True if string str (in the current language) matches the
93 regular expression regex.
94"""
95 def __init__(self):
96 super(_RegEx, self).__init__("_regex")
97
98 def invoke(self, string, regex):
99 s = string.string()
100 r = re.compile(regex.string())
101 return bool(r.match(s))
102
103
104# GDB will import us automagically via gdb/__init__.py.
105_MemEq()
106_StrLen()
107_StrEq()
108_RegEx()