如何在保留ascii形式的ascii字符的同时,使gdb以十六进制而不是八进制打印字符串的不可打印字符? [英] How can I make gdb print unprintable characters of a string in hex instead of octal while preserving the ascii characters in ascii form?
问题描述
假设我有一个缓冲区buf
,其c字符串表示为
Suppose I have a buffer buf
whose c string representation is
char* buf = "Hello World \x1c"
当我使用命令p buf
在gdb中打印此buf时,得到以下内容
When I print this buf in gdb using the command p buf
, I get the following
$1 = "Hello World \034"
是否有打印命令或gdb设置将代替打印以下内容?
$1 = "Hello World \x1c"
我尝试了各种格式参数,例如/c
和/x
,但是它们都没有达到我想要的效果.我也玩过printf,但无法达到预期的效果.
I have tried various format parameters such as /c
and /x
, but none of them get the effect that I am looking for. I have also played with printf but was unable to achieve the desired effect.
更新:我正在使用"GNU gdb(GDB)7.0.1-debian".
Update: I am using "GNU gdb (GDB) 7.0.1-debian".
更新: 我也玩过x.
Update: I have played with x as well.
如果我执行x/c
,它将打印八进制和十进制的不可打印字符,然后打印带有ascii和十进制的可打印字符.
If I do x/c
it prints octal and decimal for nonprintable characters, and then prints printable characters with the ascii and decimal.
如果执行x/s
,则其输出与p命令完全相同.
If I do x/s
it outputs exactly the same as the p command.
如果我执行x/x
,它只会输出十六进制,但是我们会丢失可打印部分的ASCII字符.
If I do x/x
it just outputs hex but then we lose the ascii characters for the printable part.
更新:此参考除非不完整,否则表明我希望无法使用,但是任何人都可以确认吗?
Update: This reference, unless incomplete, suggests that what I desire is not available, but can anyone confirm?
推荐答案
在没有现有解决方案的情况下,我创建了此gdb命令会为混合了可打印的ascii和不可打印的字符的字符串打印ascii和hex.来源转载如下.
In the absence of an existing solution, I created this gdb command which prints ascii and hex for strings that have mixed printable ascii and non-printable characters. The source is reproduced below.
from __future__ import print_function
import gdb
import string
class PrettyPrintString (gdb.Command):
"Command to print strings with a mix of ascii and hex."
def __init__(self):
super (PrettyPrintString, self).__init__("ascii-print",
gdb.COMMAND_DATA,
gdb.COMPLETE_EXPRESSION, True)
gdb.execute("alias -a pp = ascii-print", True)
def invoke(self, arg, from_tty):
arg = arg.strip()
if arg == "":
print("Argument required (starting display address).")
return
startingAddress = gdb.parse_and_eval(arg)
p = 0
print('"', end='')
while startingAddress[p] != ord("\0"):
charCode = int(startingAddress[p].cast(gdb.lookup_type("char")))
if chr(charCode) in string.printable:
print("%c" % chr(charCode), end='')
else:
print("\\x%x" % charCode, end='')
p += 1
print('"')
PrettyPrintString()
要使用此功能,只需将source AsciiPrintCommand.py
放入,然后在gdb中运行以下命令即可.为方便起见,可以将上述源代码命令放入其$HOME/.gdbinit
.
To use this, one can simply put the source AsciiPrintCommand.py
and then run the following in gdb. For convenience, one can put put the above source command into their $HOME/.gdbinit
.
ascii-print buf
"Hello World \x1c"
这篇关于如何在保留ascii形式的ascii字符的同时,使gdb以十六进制而不是八进制打印字符串的不可打印字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!