0

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".
2
  • 3
    Why are you using gdb.Function as base if you want to implement a new command? There is gdb.Command for that. But in this case I would go for the much simpler alias, something like this: alias wholestring = with print elements unlimited -- p Commented Mar 17 at 5:56
  • Thanks, that alias is actually what I need for this case, and I've seen it before, but was not able to find it either on google or in the gdb manual. Similarly, when I looked in the manual for python api options, I only found CLI Commands, which sounds to me like bash> gdb my_python_command, when actually it means (gdb) my_python_command. It is ambiguous for the manual to use the term CLI unqualified, 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. Commented Mar 17 at 12:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.