How can I make gdb print unprintable characters of a string in hex instead of octal while preserving the ascii characters in ascii form?

落爺英雄遲暮 提交于 2020-04-08 08:51:48

问题


Suppose I have a buffer buf whose c string representation is

 char* buf = "Hello World \x1c"

When I print this buf in gdb using the command p buf, I get the following

 $1 = "Hello World \034"

Is there a print command or a gdb setting that will print the following instead?

$1 = "Hello World \x1c"

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.

Update: I am using "GNU gdb (GDB) 7.0.1-debian".

Update: I have played with x as well.

If I do x/c it prints octal and decimal for nonprintable characters, and then prints printable characters with the ascii and decimal.

If I do x/s it outputs exactly the same as the p command.

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?


回答1:


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()

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"



回答2:


You might use the x command to dump the memory your string reference points to:

(gdb) x/32xb buf

shows the first 32 bytes.

See

(gdb) help x

for details.




回答3:


For anyone else who shares the irritation with octal escape-sequences in GDB, it's easy to fix (if you're prepared to build GDB yourself): in gdb/valprint.c, find the comment:

/* If the value fits in 3 octal digits, print it that
                     way.  Otherwise, print it as a hex escape.  */

and comment out the following 4 lines - all escape-sequences will then be printed as hex.




回答4:


A small variation of the OP's answer, for the use-case of 8-bit arrays that don't necessarily terminate at the first occurrence of \0, and which tries to also respect the print elements and print repeats parameters of GDB:

from __future__ import print_function

def print_extended_ascii_char(charCode):
  if charCode == ord('"'): return r'\"'
  if charCode == ord('\\'): return r'\\'
  if 32 <= charCode <= 126: return "%c" % charCode
  return r"\x%02x" % charCode

def get_gdb_value(command, output_re):
  try:
    import re
    s = gdb.execute(command, to_string=True)
    m = re.match(output_re, s)
    value = m.group(1)
    if value != 'unlimited':
      value = int(value)
    return value
  except Exception as e:
    print("Sorry, ran into an error running '%s' and getting the value." % command)
    raise e

class PrettyPrintString(gdb.Command):
    """Command to print an array of 8-bit bytes, using ASCII when possible and hex otherwise.
    https://stackoverflow.com/a/54469844/4958"""

    def __init__(self):
        super (PrettyPrintString, self).__init__(name="ascii-print",
                command_class=gdb.COMMAND_DATA,
                completer_class=gdb.COMPLETE_EXPRESSION, prefix=True)

    def invoke(self, arg, from_tty):
        if not arg.strip():
            print("What do you want me to print?")
            return
        limit = get_gdb_value('show print elements', 'Limit on string chars or array elements to print is (.*).\n')
        repeats = get_gdb_value('show print repeats', 'Threshold for repeated print elements is (.*).\n')
        start = gdb.parse_and_eval(arg)
        p = 0
        print('"', end='')
        i = 0
        unprinted = (None, 0)
        while i < start.type.sizeof:
            i += 1
            charCode = int(start[p])
            toPrint = print_extended_ascii_char(charCode)
            if toPrint == unprinted[0]:
              unprinted = (toPrint, unprinted[1] + 1)
            else:
              if unprinted[0] is not None:
                print(unprinted[0] * min(unprinted[1], limit - (i - unprinted[1])), end='')
                if i > limit:
                  print('...', end='')
                  break
              unprinted = (toPrint, 1)
            p += 1
        if i - unprinted[1] > limit or unprinted[0] is None:
          print('"')
        elif repeats == 'unlimited' or unprinted[1] < repeats:
          print(unprinted[0] * unprinted[1], end='')
          print('"')
        else:
          print('",')
          print("'%s' <repeats %d times>" % (unprinted[0], unprinted[1] - 1))

PrettyPrintString()

As before, put the above in some file (say ~/.gdb-AsciiPrint.py), and either run source ~/.gdb-AsciiPrint.py or put that statement in the .gdbinit file. Result/comparison:

(gdb) p tokmem[3]
$1 = "\000\030\000-1\320\a\200\031:\200\032[\200\024]\200\033\200\023;\200\034:\200\032[\200\023]\200\033\200\024;\320\r\200$:\200\030;\320\020\200 k\030\060\200!255\200\"\200\r\200\060(k:3,': \"');l\030k;\200+(\250\061)\200,\200\r\200\060(\200\034[\200\062],\200\034[\200\062]);\200+k<\f100\200,l\030k+\f100\200.\200+k<\f200\200,l\030k-\f100\200.\200\r\200*(k\200\063\061\066);\200\060(\200\034[l]);\200*(k\200\064\061\066);\200\020(\200$);\200\017;\200$\030\200$+2;\200\017;\200+l=\200\065\200,\200\060(\200\034[l],\200\034[l])\200.\200\060(\200\034[l]);\200\020(\200$);\200&('\"');\200\017", '\000' <repeats 65285 times>
(gdb) ascii-print tokmem[3]
"\x00\x18\x00-1\xd0\x07\x80\x19:\x80\x1a[\x80\x14]\x80\x1b\x80\x13;\x80\x1c:\x80\x1a[\x80\x13]\x80\x1b\x80\x14;\xd0\x0d\x80$:\x80\x18;\xd0\x10\x80 k\x180\x80!255\x80\"\x80\x0d\x800(k:3,': \"');l\x18k;\x80+(\xa81)\x80,\x80\x0d\x800(\x80\x1c[\x802],\x80\x1c[\x802]);\x80+k<\x0c100\x80,l\x18k+\x0c100\x80.\x80+k<\x0c200\x80,l\x18k-\x0c100\x80.\x80\x0d\x80*(k\x80316);\x800(\x80\x1c[l]);\x80*(k\x80416);\x80\x10(\x80$);\x80\x0f;\x80$\x18\x80$+2;\x80\x0f;\x80+l=\x805\x80,\x800(\x80\x1c[l],\x80\x1c[l])\x80.\x800(\x80\x1c[l]);\x80\x10(\x80$);\x80&('\"');\x80\x0f",
'\x00' <repeats 65285 times>

This is slightly hacky, so hopefully the feature will get added to GDB itself.

(Aside: A while ago I asked a similar question for Emacs, and one of the people who saw the question submitted a patch to Emacs. It does seem that octal is less popular these days than it used to be; e.g. JavaScript has deprecated octal escape sequences for its string literals.)



来源:https://stackoverflow.com/questions/16031100/how-can-i-make-gdb-print-unprintable-characters-of-a-string-in-hex-instead-of-oc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!