简体   繁体   中英

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 :

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 ?

Obs:

When i print name , lldb give me this type std::__1::string for 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...

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. Since we are getting our information about the type from the DWARF debug info, we are restricted by what it represents. 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.

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

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. It could be an issue linked to the address sanitizer.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM