Skip to content

2.68 在GDB中拼接字符串

https://scz.617.cn/unix/201612011200.txt

Q:

如何在GDB脚本中或GDB提示符下拼接字符串?

A: Craig Ringer 2016-12-01

Concatenate GDB convenience variables - [2016-12-01] https://stackoverflow.com/questions/40917677/concatenate-gdb-convenience-variables


import gdb

class Concat ( gdb.Function ) :

def __init__ ( self ) :
    super( Concat, self ).__init__( "concat" )

def _unwrap_string ( self, v ) :
    try:
        return v.string()
    except gdb.error :
        return str( v )

def invoke ( self, *args ) :
    return ''.join( [self._unwrap_string(x) for x in args] )

Concat()

(gdb) source concat.py (gdb) set $x=$concat("works", " ", "when", " ", "detached") (gdb) p $x $1 = "works when detached"

D: 付程 2021-05-28

define concat2 set $ret=0 set $ret=$arg0 $arg1 end concat2 "aaa" "bbbb" p $ret

"set $ret=0"的意图是"重置",以此避免下一条set触发"Too many array elements"

参看:

https://github.com/basinilya/wisdom/blob/master/prog/gdb/gdb-faq.txt

GDB直接支持字符串拼接,擦,以前不知道可以这样写。这样的话,就不需要concat 函数或者define,原始需求GDB直接满足。

set $any=0 set $any="aaa" " " "bbbb" " " "ccccc" p $any

输出"aaa bbbb ccccc"