]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/lib/gdb/function/strfns.py
Update copyright year range in all GDB files.
[thirdparty/binutils-gdb.git] / gdb / python / lib / gdb / function / strfns.py
CommitLineData
a72c3253 1# Useful gdb string convenience functions.
b811d2c2 2# Copyright (C) 2012-2020 Free Software Foundation, Inc.
a72c3253
DE
3
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):
590042fc 24 """$_memeq - compare bytes of memory.
a72c3253 25
2fb009bb 26Usage: $_memeq (A, B, LEN)
a72c3253
DE
27
28Returns:
2fb009bb 29 True if LEN bytes at A and B compare equally."""
a72c3253
DE
30 def __init__(self):
31 super(_MemEq, self).__init__("_memeq")
32
33 def invoke(self, a, b, length):
34 if length < 0:
35 raise ValueError("length must be non-negative")
36 if length == 0:
37 return True
38 # The argument(s) to vector are [low_bound,]high_bound.
39 byte_vector = gdb.lookup_type("char").vector(length - 1)
40 ptr_byte_vector = byte_vector.pointer()
41 a_ptr = a.reinterpret_cast(ptr_byte_vector)
42 b_ptr = b.reinterpret_cast(ptr_byte_vector)
43 return a_ptr.dereference() == b_ptr.dereference()
44
45
46class _StrLen(gdb.Function):
590042fc 47 """$_strlen - compute string length.
a72c3253 48
2fb009bb 49Usage: $_strlen (A)
a72c3253
DE
50
51Returns:
2fb009bb 52 Length of string A, assumed to be a string in the current language."""
a72c3253
DE
53 def __init__(self):
54 super(_StrLen, self).__init__("_strlen")
55
56 def invoke(self, a):
57 s = a.string()
58 return len(s)
59
60
61class _StrEq(gdb.Function):
590042fc 62 """$_streq - check string equality.
a72c3253 63
2fb009bb 64Usage: $_streq (A, B)
a72c3253
DE
65
66Returns:
2fb009bb 67 True if A and B are identical strings in the current language.
a72c3253
DE
68
69Example (amd64-linux):
70 catch syscall open
2fb009bb 71 cond $bpnum $_streq((char*) $rdi, "foo")"""
a72c3253
DE
72 def __init__(self):
73 super(_StrEq, self).__init__("_streq")
74
75 def invoke(self, a, b):
76 return a.string() == b.string()
77
78
79class _RegEx(gdb.Function):
590042fc 80 """$_regex - check if a string matches a regular expression.
a72c3253 81
2fb009bb 82Usage: $_regex (STRING, REGEX)
a72c3253
DE
83
84Returns:
2fb009bb
TT
85 True if string STRING (in the current language) matches the
86 regular expression REGEX."""
a72c3253
DE
87 def __init__(self):
88 super(_RegEx, self).__init__("_regex")
89
90 def invoke(self, string, regex):
91 s = string.string()
92 r = re.compile(regex.string())
93 return bool(r.match(s))
94
95
96# GDB will import us automagically via gdb/__init__.py.
97_MemEq()
98_StrLen()
99_StrEq()
100_RegEx()