在GDB中创建C ++字符串 [英] Creating C++ string in GDB

查看:255
本文介绍了在GDB中创建C ++字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在GDB中创建 std :: string (或任何C ++对象)。我尝试了很多变化,以下并没有一个似乎工作:

pre $ g $ c pd std :: string( hello)
表达式中的语法错误,靠近hello)。

有没有办法做到这一点?

< (我很惊讶,我在Web上找不到任何东西,我开始考虑我的GDB是否有问题,或者我做错了什么。)


$ $ $ $ $ $ $ $ $ $ $($)$($) 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 =<优化出>,_M_dataplus = {< std :: allocator< char>> = {< __ gnu_cxx :: new_allocator< char>> = {<没有数据字段>},<没有数据字段>},_M_p = 0x91a6f8Hello,World}}
(gdb)call SomeFunctionThatTakesAConstStringRef(*(const std :: string *)0x91a6a0)


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.)

解决方案

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)

这篇关于在GDB中创建C ++字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆