创建 C++ GDB 中的字符串

发布于 2024-12-04 17:27:34 字数 263 浏览 1 评论 0原文

我在 GDB 中创建 std::string (或任何 C++ 对象,我猜)时遇到问题。我尝试了以下多种变体,但似乎都不起作用:

(gdb) p std::string("hello")
A syntax error in expression, near `"hello")'.

有办法做到这一点吗?

(我很惊讶我在网上找不到任何关于此的信息。我开始思考我的 GDB 是否有问题或者我做错了什么。)

I'm having trouble creating an std::string (or any C++ object, I guess) in GDB. I tried lots of variations to the following and none of them seem to work:

(gdb) p std::string("hello")
A syntax error in expression, near `"hello")'.

Is there a way to do it?

(I'm surprised I couldn't find anything about this on the Web. I'm starting to think if my GDB is buggy or I'm doing something very wrong.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他夏了夏天 2024-12-11 17:27:34

您应该能够在 GDB 中构造一个新的 std::string 。您想要在堆上分配空间来保存 std::string 对象、调用默认构造函数并分配字符串值。这是一个例子:

(gdb) call malloc(sizeof(std::string))
$1 = (void *) 0x91a6a0
(gdb) call ((std::string*)0x91a6a0)->basic_string()
(gdb) call ((std::string*)0x91a6a0)->assign("Hello, World")
$2 = (std::basic_string<char, std::char_traits<char>, std::allocator<char> > &) @0x91a6a0: {static npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x91a6f8 "Hello, World"}}
(gdb) call SomeFunctionThatTakesAConstStringRef(*(const std::string*)0x91a6a0)

You should be able to construct a new std::string within the GDB. You want to allocate space on the heap to hold the std::string object, invoke the default constructor, and assign your string value. Here is an example:

(gdb) call malloc(sizeof(std::string))
$1 = (void *) 0x91a6a0
(gdb) call ((std::string*)0x91a6a0)->basic_string()
(gdb) call ((std::string*)0x91a6a0)->assign("Hello, World")
$2 = (std::basic_string<char, std::char_traits<char>, std::allocator<char> > &) @0x91a6a0: {static npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x91a6f8 "Hello, World"}}
(gdb) call SomeFunctionThatTakesAConstStringRef(*(const std::string*)0x91a6a0)
羅雙樹 2024-12-11 17:27:34

GDB 不能真正做到你所描述的。您的案例涉及:

  1. 实例化 basic_string 模板并为类
  2. 生成代码 生成对构造函数的调用

这意味着它必须完成与编译器相同复杂性的工作。这不是调试器的工作。

话虽如此,GDB 能够评估语句的有限子集,例如使用现有数据调用现有函数并检索其结果,因为这不会涉及生成很多代码。

GDB cannot really do what you describe. Your case involves:

  1. instantiating a basic_string template and generating code for the class
  2. generate a call to constructor

This means it must do the work of the same complexity as a compiler. This is not the job of the debugger.

With that said, GDB is capable of evaluating a limited subset of statements, like calling an existing function with existing data and retrieving its result, since this won't involve generating a lot of code.

樱桃奶球 2024-12-11 17:27:34

你所说的“创造”是什么意思? GDB 不会持久保存 C++ 对象(您的应用程序会这样做),因此您无法在 GDB 本身中创建 C++ 对象。

但是,您应该能够从 GDB 调用应用程序的特定函数。

What do you mean by "creating"? GDB doesn't persist C++ objects (your application does that), so you can't create a C++ object in GDB itself.

However, you should be able to call specific function of your application from GDB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文