admin管理员组

文章数量:1389757

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 invoke custom python command without printing itStack Overflow