Since gdb has only one global set print elements that affects all array-like types, it has been necessary to use a very short value like set print elements 4. But in certain specific cases it can be useful to see the entire string without changing the global setting:
(gdb) p "a string longer than the current 'print elements'"
$3 = "a st"...
Adding a python command is simple enough:
import gdb
class WholeString(gdb.Function):
def __init__(self):
super(WholeString, self).__init__("wholestring")
def invoke(self, m):
elements = gdb.execute("show print elements", True, True)[:-2].split(" ")[-1]
gdb.execute("set print elements 0")
print("[whole entire string: {}]".format(m))
if int(elements) > 0:
gdb.execute("set print elements {}".format(elements))
return ""
WholeString()
While this works, the only way to invoke the wholestring command is by printing it, which litters the terminal with a useless empty string:
(gdb) p $wholestring("a string longer than the current 'print elements'")
[whole entire string: "a string longer than the current 'print elements'"]
$2 = ""
Same result for (gdb) call $wholestring("..."). Is there any way to invoke the command without printing it?
(gdb) $wholestring("a string longer than the current 'print elements'")
Undefined command: "$wholestring". Try "help".
gdb.Functionas base if you want to implement a new command? There isgdb.Commandfor that. But in this case I would go for the much simpler alias, something like this:alias wholestring = with print elements unlimited -- pCLI Commands, which sounds to me likebash> gdb my_python_command, when actually it means(gdb) my_python_command. It is ambiguous for the manual to use the termCLIunqualified, and nothing in the top of that page indicated for me that we were talking about the gdb internal CLI and not the system CLI from which gdb is started.