C++ - How to see content of std::string

1,122 views
Skip to first unread message

Puya Daravi

unread,
Feb 11, 2021, 10:19:52 PM2/11/21
to CodeLLDB Users

Hello, I know that in the debugger console I can run
expr str.data()

To see the content of a std::string object. Is there anyway I could setup CodeLLDB so that the string is shown when I hover over the variable name?

Vadim Chugunov

unread,
Feb 12, 2021, 1:35:21 PM2/12/21
to Puya Daravi, CodeLLDB Users
Which OS/compiler?   LLDB uses variable formatters to display values of complex objects.   For some of the STL types (string is among those) these formatters come bundled with LLDB.  However, this covers only GCC and CLang standard libraries; there are no formatters for MSVC, for example.

--
You received this message because you are subscribed to the Google Groups "CodeLLDB Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codelldb-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/codelldb-users/314a55b9-09ae-4963-85ec-2b278bbc8037n%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Puya Daravi

unread,
Feb 12, 2021, 3:49:54 PM2/12/21
to CodeLLDB Users
Hello Vadim,

That is good to know thank you. I am on CentOS 8 and using LLVM 10.0.1 and I link with libc++. Maybe I am missing a compile option for LLVM/LLDB to include these pre-bundled formatters? Is there a way for me to check if these formatters are there in my installation?

Thank you,
Puya

Vadim Chugunov

unread,
Feb 12, 2021, 7:16:27 PM2/12/21
to Puya Daravi, CodeLLDB Users
Try "type category enable libcxx"

Puya Daravi

unread,
Mar 11, 2021, 2:24:43 AM3/11/21
to CodeLLDB Users
Hello Vadim,

Thank you for the tip. This gives me:
```
(lldb) type category enable libcxx
warning: empty category enabled (typo?)
```
Does this mean that my lldb installation is missing the libcxx category formatters?
Here is what I get when I start up lldb:
```
(lldb) type category list
Category: default (enabled)
Category: VectorTypes (enabled, applicable for language(s): objective-c++)
Category: system (enabled, applicable for language(s): objective-c++)
```
I see that the file you mentioned is here:
https://github.com/llvm-mirror/lldb/blob/master/examples/synthetic/libcxx.py

Do you know how I can install this on my system so that LLDB has this loaded by default?

Thank you,
Puya

Puya Daravi

unread,
Mar 11, 2021, 2:59:25 AM3/11/21
to CodeLLDB Users
Actually the above was for when I start lldb from the command line. The lldb session started by CodeLLDB returns this:
```
-> type category list
Category: default (enabled)
Category: Rust (enabled)

Category: VectorTypes (enabled, applicable for language(s): objective-c++)
Category: system (enabled, applicable for language(s): objective-c++)
Category: cplusplus (enabled)
Category: objc (enabled)
```

I then added ~/.lldbinit to load the libc++ formatter:
```
command script import ~/lldb-categories/libcxx.py
```
And I see that now the category is enabled when I run lldb from the command line. But it is not added to the lldb session that is launched by the CodeLLDB plugin.

How can I have the plugin load this file as well?

P.S. I have also tried adding this to my `launch.json` file:
```
"initCommands": [
    "command script import /home/puya/lldb-categories/libcxx.py"
],
```

I now see `Category: libcxx (enabled)` listed in when I start CodeLLDB. However, the std::string variable still shows as "Summary Unavailable" (and `p var` also shows the same).

Regards,
Puya

Puya Daravi

unread,
Mar 11, 2021, 3:12:27 AM3/11/21
to CodeLLDB Users
Hello,

Sorry for the multiple messages. One more thing. When I run lldb via command line I get an empty string instead of the correct value:

```
(lldb) type category list
Category: default (enabled)
Category: libcxx (enabled)

Category: VectorTypes (enabled, applicable for language(s): objective-c++)
Category: system (enabled, applicable for language(s): objective-c++)
Category: cplusplus (enabled)
Category: objc (enabled)
(lldb) s
Process 30813 stopped
* thread #1, name = 'a.out', stop reason = step in
    frame #0: 0x0000000000400df4 a.out`main(argc=1, argv=0x00007fffffffcd68) at hello.cpp:7:14
   4    int main(int argc, char const *argv[])
   5    {
   6            std::string s1{ "hello" };
-> 7            std::string s2{ "world" };
   8  
   9            std::cout << s1 << " " << s2 << "!\n";
   10           return 0;
(lldb) p s1
(std::__1::string) $0 = ""
```

Puya

Vadim Chugunov

unread,
Apr 8, 2021, 12:24:30 AM4/8/21
to Puya Daravi, CodeLLDB Users
If you are building with clang, please see this.

Reply all
Reply to author
Forward
0 new messages