简体   繁体   English

如何在 lldb 上设置 std::string 类型?

[英]How set std::string type on lldb?

I have the following code:我有以下代码:

#include <iostream>
#include <string>

int main() {
    std::string name = "Foo";
    return 0;
}

When i will debug with lldb i want set a std::string like i set this int :当我使用lldb调试时,我想设置一个std::string就像我设置这个int

expr int $num = 4

I try:我尝试:

expr std::string $name = "Foo"

And i got:我得到了:

error: no type named 'string' in namespace 'std'

Does anyone understand what is going on?有谁明白发生了什么? How can i create a std::string on lldb ?如何在lldb上创建std::string

Obs:观察:

When i print name , lldb give me this type std::__1::string for std::string :当我打印name ,lldb 为std::string给我这种类型的std::__1::string std::string

p foo
(std::__1::string) $2 = "foo"

std::string looks simple from the outside but it's actually a quite complex little beast... std::string 从外面看起来很简单,但它实际上是一个相当复杂的小野兽......

For instance, all the std::string constructors except the copy constructors take defaulted arguments (allocators and the like) that most normal C++ users never override, and so don't know about.例如,除复制构造函数之外的所有 std::string 构造函数都采用大多数普通 C++ 用户从未覆盖的默认参数(分配器等),因此不知道。 Since we are getting our information about the type from the DWARF debug info, we are restricted by what it represents.由于我们是从 DWARF 调试信息中获取有关类型的信息,因此我们受到它所代表的内容的限制。 It doesn't represent the values of defaulted arguments, however, so we don't know how to fill them in.然而,它不代表默认参数的值,所以我们不知道如何填写它们。

You can try a little harder by reading the string header, and you figure out you need to provide a size and an allocator, and get to something like:您可以通过阅读字符串标头更努力地尝试,并且您发现您需要提供大小和分配器,并获得如下内容:

expr std::string $myStr("Foo", 4, std::allocator<char>())
error: Couldn't lookup symbols:
  __ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEC1EPKcmRKS4_

That fails because the allocator is everywhere inlined, so lldb can't find a constructor to call for it.那失败了,因为分配器到处都是内联的,所以 lldb 找不到调用它的构造函数。

The real solution to this is to have lldb rebuild the std module from headers, import the resultant clang module into lldb's compiler and use that to create these values.真正的解决方案是让 lldb 从头文件重建 std 模块,将生成的 clang 模块导入 lldb 的编译器并使用它来创建这些值。 At that point we'd have the information needed to do this job.那时,我们将拥有完成这项工作所需的信息。 For instance we would know about defaulted arguments, and could instantiate the missing allocator from the module.例如,我们会知道默认参数,并且可以从模块中实例化丢失的分配器。

But building a Clang Module from the c++ std headers turns out not to be entirely trivial, so we're still working on that...但是从 c++ 标准头文件构建一个 Clang 模块结果证明并不是完全微不足道的,所以我们仍在努力......

You can use alternatively:您可以选择使用:

  • expr const char * $foo
  • expr const char $foo[]
  • expr const char $foo[4]

Obviously the last one can be used only if you know the dimension of the char-array显然只有在知道字符数组的维数时才能使用最后一个

What kind of a compiler are you using?您使用的是哪种编译器? From what I found this issue exists with clang 5.0.1 but not with clang 7.0.0.从我发现这个问题存在于 clang 5.0.1 但不存在于 clang 7.0.0。 It could be an issue linked to the address sanitizer.这可能是与地址清理程序相关的问题。

Try brew install llvm --with-toolchain --with-lldb尝试brew install llvm --with-toolchain --with-lldb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问地图<std::string, int>元素使用 lldb? - How to access map<std::string, int> element using lldb? C ++:LLDB + Python-如何在python脚本中打印std :: string - c++: LLDB + Python - how to print a std::string in the python script 打印 std::vector 的内容<std::string>在 lldb 中</std::string> - print contents of std::vector<std::string> in lldb 使用LLDB检查VSCode中的C ++ std :: set - inspect C++ std::set in VSCode with LLDB 无法检查 lldb 中的 std::string 变量 - Cannot inspect a std::string variable in lldb 如何初始化std :: set <std::string> 是否正确? - How to initializing std::set<std::string> correctly? 如何检查是否设置了std :: string? - How to check if a std::string is set or not? C ++ std ::地图 <std::string, std::set<std::string> &gt;。 如何循环设置值? - c++ std::map<std::string, std::set<std::string>> . How to loop set values? 如何设置std :: vector <std::string> 每个元素中都有自定义字符串 - How to set std::vector<std::string> with custom string in each element c ++ :: libc ++中的字符串库出错abi.dylib:以std :: out_of_range类型的未捕获异常终止:basic_string(lldb) - Error with string library in c++ :: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string (lldb)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM