mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2026-08-27 00:26:02 -04:00
This commit adds a new method gdb.Symtab.source_lines. This method can be used to read the lines from a symtab's source file. This is similar to GDB's internal source_cache::get_source_lines function. Currently using the Python API, if a user wants to display source lines then they need to use Symtab.fullname() to get the source file name, then open this file and parse out the lines themselves. This isn't too much effort, but the problem is that these lines will not be styled. The user could style the source content themselves, but will this be styled exactly as GDB would style it? The new Symtab.source_lines() method returns source lines with styling included (as ANSI terminal escape sequences), assuming of course, that styling is currently enabled. Of course, in some cases, a user of the Python API might want source code without styling. That's supported too, the new method has an 'unstyled' argument. If this is True then the output is forced to be unstyled. The argument is named 'unstyled' rather than 'styled' because the API call cannot force styling on. If 'set style enabled off' is in effect then making the API call will never return styled source lines. The new API call allows for a range of lines to be requested if desired. As part of this commit I've updated the host_string_to_python_string utility function to take a std::string_view. Reviewed-By: Eli Zaretskii <eliz@gnu.org> Approved-By: Tom Tromey <tom@tromey.com>
77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
# Copyright (C) 2026 Free Software Foundation, Inc.
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import gdb
|
|
|
|
|
|
class PythonListCommand(gdb.Command):
|
|
def __init__(self):
|
|
gdb.Command.__init__(self, "py-list", gdb.COMMAND_USER)
|
|
|
|
def invoke(self, args, from_tty):
|
|
argv = gdb.string_to_argv(args)
|
|
assert len(argv) == 0 or len(argv) == 1
|
|
|
|
pc = gdb.parse_and_eval("$pc")
|
|
sal = gdb.current_progspace().find_pc_line(pc)
|
|
assert sal.symtab is not None
|
|
|
|
if len(argv) == 0:
|
|
source_lines = sal.symtab.source_lines()
|
|
start = 1
|
|
else:
|
|
parts = argv[0].split(",")
|
|
source_lines = sal.symtab.source_lines(
|
|
first=int(parts[0]), last=int(parts[1])
|
|
)
|
|
start = int(parts[0])
|
|
if source_lines is None:
|
|
print("Source file {} not found.".format(sal.symtab.fullname()))
|
|
else:
|
|
lns = gdb.Style("line-number")
|
|
for i, l in enumerate(source_lines, start=start):
|
|
print("%s\t%s" % (lns.apply(str(i)), l), end="")
|
|
|
|
|
|
class PythonListUnstyledCommand(gdb.Command):
|
|
def __init__(self):
|
|
gdb.Command.__init__(self, "py-list-unstyled", gdb.COMMAND_USER)
|
|
|
|
def invoke(self, args, from_tty):
|
|
argv = gdb.string_to_argv(args)
|
|
assert len(argv) == 0 or len(argv) == 1
|
|
|
|
pc = gdb.parse_and_eval("$pc")
|
|
sal = gdb.current_progspace().find_pc_line(pc)
|
|
assert sal.symtab is not None
|
|
|
|
if len(argv) == 0:
|
|
source_lines = sal.symtab.source_lines(unstyled=True)
|
|
start = 1
|
|
else:
|
|
parts = argv[0].split(",")
|
|
source_lines = sal.symtab.source_lines(
|
|
first=int(parts[0]), last=int(parts[1]), unstyled=True
|
|
)
|
|
start = int(parts[0])
|
|
if source_lines is None:
|
|
print("Source file {} not found.".format(sal.symtab.fullname()))
|
|
else:
|
|
for i, l in enumerate(source_lines, start=start):
|
|
print("%d\t%s" % (i, l), end="")
|
|
|
|
|
|
PythonListCommand()
|
|
PythonListUnstyledCommand()
|