You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GDB调用带std::string参数函数报错无法解析重载实例如何正确传参

报错原因

GDB的C++重载解析逻辑无法匹配函数的实际二进制签名:

  • 你通过print FUNCTION看到的std::string (std::string)是语法层面的C签名,而GCC遵循Itanium C ABI编译时,返回std::string这类非平凡类型的函数会自动插入隐含的第一个参数,即返回值的存储地址指针,也就是Ghidra标注的string *__return_storage_ptr__,实际函数的二进制签名为std::string* (*)(std::string*, std::string*),和GDB识别的语法层签名不匹配,导致解析失败。

正确调用方法

你之前构造参数的逻辑是正确的,只需要绕开GDB的重载解析,直接用你已经获取到的函数地址(0x564803a0b545)手动指定类型调用即可:

# 构造输入参数
set $param2 = (std::string *)malloc(sizeof(std::string))
call $param2->basic_string()
call $param2->assign("ASDASDASD")

# 申请返回值存储缓冲区
set $ret_buf = (std::string *)malloc(sizeof(std::string))

# 核心:将地址强转为匹配二进制签名的函数指针后调用
call ((std::string* (*)(std::string*, std::string*))0x564803a0b545)($ret_buf, $param2)

# 调用完成后可打印返回结果验证
print *$ret_buf

优化技巧

如果需要多次调用该函数,可以提前将强转后的函数指针存入GDB变量,简化后续调用:

set $MY_FUNC = ((std::string* (*)(std::string*, std::string*))0x564803a0b545)
# 后续直接调用即可
call $MY_FUNC($ret_buf, $param2)

内存清理提示

调用完成后建议手动析构并释放申请的std::string对象,避免内存泄漏:

call $ret_buf->~basic_string()
call free($ret_buf)
call $param2->~basic_string()
call free($param2)

内容的提问来源于stack exchange,提问作者ArandomDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 18:36:07