forked from gramineproject/gramine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage_gdb.py
59 lines (40 loc) · 1.8 KB
/
language_gdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (C) 2021 Invisible Things Lab
# Michał Kowalczyk <[email protected]>
# Commands for temporarily changing source language. Used by other Gramine GDB scripts to ensure
# that a specific source language is used for parsing expressions - GDB interprets scripts using
# the language taken from currently executing code, which may change in time, resulting in scripts
# working only part of the time.
#
# Example: When a GDB script does `if *(uint16_t*)$rip == 0x1234` in a signal catchpoint then it
# will fail with a syntax error if the binary debugged is written in Rust, but only if the signal
# arrived while executing Rust code.
import re
import gdb # pylint: disable=import-error
_g_languages = []
class PushLanguage(gdb.Command):
"""Temporarily change source language and save the old one"""
def __init__(self):
super().__init__('push-language', gdb.COMMAND_USER)
def invoke(self, arg, _from_tty):
self.dont_repeat()
lang_str = gdb.execute('show language', to_string=True).strip()
# ';' is for things like: "auto; currently c"
m = re.match(r'The current source language is "(.*?)[";]', lang_str)
assert m, 'Unexpected output from \'show language\': ' + lang_str
_g_languages.append(m.group(1))
gdb.execute('set language ' + arg)
class PopLanguage(gdb.Command):
"""Recover source language saved by PushLanguage"""
def __init__(self):
super().__init__('pop-language', gdb.COMMAND_USER)
def invoke(self, arg, _from_tty):
self.dont_repeat()
assert arg == ''
lang = _g_languages.pop()
gdb.execute('set language ' + lang)
def main():
PushLanguage()
PopLanguage()
if __name__ == '__main__':
main()