]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/lib/gdb/function/strfns.py
gas: introduce .errif and .warnif
[thirdparty/binutils-gdb.git] / gdb / python / lib / gdb / function / strfns.py
CommitLineData
a72c3253 1# Useful gdb string convenience functions.
d01e8234 2# Copyright (C) 2012-2025 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
a72c3253
DE
19import re
20
c2cf30e7
TT
21import gdb
22
a72c3253
DE
23
24class _MemEq(gdb.Function):
13123da8 25 """$_memeq - compare bytes of memory.
a72c3253 26
13123da8 27 Usage: $_memeq (A, B, LEN)
a72c3253 28
13123da8
SM
29 Returns:
30 True if LEN bytes at A and B compare equally."""
a72c3253 31
13123da8
SM
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()
a72c3253
DE
46
47
48class _StrLen(gdb.Function):
13123da8
SM
49 """$_strlen - compute string length.
50
51 Usage: $_strlen (A)
a72c3253 52
13123da8
SM
53 Returns:
54 Length of string A, assumed to be a string in the current language."""
a72c3253 55
13123da8
SM
56 def __init__(self):
57 super(_StrLen, self).__init__("_strlen")
a72c3253 58
13123da8
SM
59 def invoke(self, a):
60 s = a.string()
61 return len(s)
a72c3253
DE
62
63
64class _StrEq(gdb.Function):
13123da8 65 """$_streq - check string equality.
a72c3253 66
13123da8 67 Usage: $_streq (A, B)
a72c3253 68
13123da8
SM
69 Returns:
70 True if A and B are identical strings in the current language.
a72c3253 71
13123da8
SM
72 Example (amd64-linux):
73 catch syscall open
74 cond $bpnum $_streq((char*) $rdi, "foo")"""
a72c3253 75
13123da8
SM
76 def __init__(self):
77 super(_StrEq, self).__init__("_streq")
78
79 def invoke(self, a, b):
80 return a.string() == b.string()
a72c3253
DE
81
82
83class _RegEx(gdb.Function):
13123da8
SM
84 """$_regex - check if a string matches a regular expression.
85
86 Usage: $_regex (STRING, REGEX)
a72c3253 87
13123da8
SM
88 Returns:
89 True if string STRING (in the current language) matches the
90 regular expression REGEX."""
a72c3253 91
13123da8
SM
92 def __init__(self):
93 super(_RegEx, self).__init__("_regex")
a72c3253 94
13123da8
SM
95 def invoke(self, string, regex):
96 s = string.string()
97 r = re.compile(regex.string())
98 return bool(r.match(s))
a72c3253
DE
99
100
101# GDB will import us automagically via gdb/__init__.py.
102_MemEq()
103_StrLen()
104_StrEq()
105_RegEx()