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