GDB (xrefs)
Loading...
Searching...
No Matches
styling.py
Go to the documentation of this file.
1# Styling related hooks.
2# Copyright (C) 2010-2023 Free Software Foundation, Inc.
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"""Utilities for styling."""
18
19import gdb
20
21try:
22 from pygments import formatters, lexers, highlight
23 from pygments.token import Error, Comment, Text
24 from pygments.filters import TokenMergeFilter
25
26 _formatter = None
27
28 def get_formatter():
29 global _formatter
30 if _formatter is None:
31 _formatter = formatters.TerminalFormatter()
32 return _formatter
33
34 def colorize(filename, contents):
35 # Don't want any errors.
36 try:
37 lexer = lexers.get_lexer_for_filename(filename, stripnl=False)
38 formatter = get_formatter()
39 return highlight(contents, lexer, formatter).encode(
40 gdb.host_charset(), "backslashreplace"
41 )
42 except:
43 return None
44
45 class HandleNasmComments(TokenMergeFilter):
46 @staticmethod
47 def fix_comments(lexer, stream):
48 in_comment = False
49 for ttype, value in stream:
50 if ttype is Error and value == "#":
51 in_comment = True
52 if in_comment:
53 if ttype is Text and value == "\n":
54 in_comment = False
55 else:
56 ttype = Comment.Single
57 yield ttype, value
58
59 def filter(self, lexer, stream):
60 f = HandleNasmComments.fix_comments
61 return super().filter(lexer, f(lexer, stream))
62
63 _asm_lexers = {}
64
65 def __get_asm_lexer(gdbarch):
66 lexer_type = "asm"
67 try:
68 # For an i386 based architecture, in 'intel' mode, use the nasm
69 # lexer.
70 flavor = gdb.parameter("disassembly-flavor")
71 if flavor == "intel" and gdbarch.name()[:4] == "i386":
72 lexer_type = "nasm"
73 except:
74 # If GDB is built without i386 support then attempting to fetch
75 # the 'disassembly-flavor' parameter will throw an error, which we
76 # ignore.
77 pass
78
79 global _asm_lexers
80 if lexer_type not in _asm_lexers:
81 _asm_lexers[lexer_type] = lexers.get_lexer_by_name(lexer_type)
82 _asm_lexers[lexer_type].add_filter(HandleNasmComments())
83 _asm_lexers[lexer_type].add_filter("raiseonerror")
84 return _asm_lexers[lexer_type]
85
86 def colorize_disasm(content, gdbarch):
87 # Don't want any errors.
88 try:
89 lexer = __get_asm_lexer(gdbarch)
90 formatter = get_formatter()
91 return highlight(content, lexer, formatter).rstrip().encode()
92 except:
93 return content
94
95except:
96
97 def colorize(filename, contents):
98 return None
99
100 def colorize_disasm(content, gdbarch):
101 return None
void f()
Definition 1.cc:36
fix_comments(lexer, stream)
Definition styling.py:47
filter(self, lexer, stream)
Definition styling.py:59
__get_asm_lexer(gdbarch)
Definition styling.py:65
colorize_disasm(content, gdbarch)
Definition styling.py:86
colorize(filename, contents)
Definition styling.py:34
get_formatter()
Definition styling.py:28