Print string with newlines with lldb
Asked Answered
C

2

9

I'd like to print a string, either const char* or std::string, using lldb so that it is human readable. Most importantly, \n's would be printed as a newline. Does anyone know how to do this? I tried the advice given for gdb in this post, however it doesn't seem to work with lldb.

Edit: I'm aware that you can issue the print myString command to print the string, however it doesn't format newline characters (at least not by default):

Chuddar answered 14/12, 2015 at 23:5 Comment(4)
What is wrong with the information presented on the page that comes up as the first result on a Google search for lldb C++ print string? Is it because the "printable character"/'C' formatter doesn't expand the newline? Why do you think LLDB is capable of anything further than what is detailed on this remarkably extensive documentation page?Scary
Extensive documentation? LLDB's documentation pages are one of the most difficult things to follow that I've come across. I virtually never find what I'm looking for.Chuddar
And yes, the lldb print command doesn't print a formatted string. I edited my post to state that obvious point.Chuddar
Well the page I refer to seems clear and extensive enough...Scary
L
6

Most of the time you want to see the literal contents of your strings, so the default lldb behavior for print is correct. However, it would be useful to have a format option to "render" the output in the same way the standard libraries would do a string. That's basically what the gdb "printf" command is. Please file a bug with the lldb.llvm.org bug reporter asking for this.

Just like with gdb, you can get the standard library to render the text for you:

(lldb)  expr (void) printf("Some text\nMore text\nEven more text\n")
Some text
More text
Even more text
(lldb)

I cast it to void in this case because I didn't care about the return value, and it makes it harder to see the text.

As was pointed out in the post you referred to, if your output is not going to a terminal somewhere, that isn't helpful, so some explicit "render the output" option would be a good idea as well. But that should only happen if you attach to, rather than run, your program in the debugger.

Laruelarum answered 15/12, 2015 at 19:35 Comment(0)
D
3

As of 2023, you can now do this with settings set escape-non-printables false. That will print all strings without escaping newlines.

Duero answered 24/3, 2023 at 15:19 Comment(2)
The correct command is "settings" (plural). I wish I could just edit the answer, but the minimum edit is at least 6 characters.Counterirritant
lldb's command line always uses the "shortest unique completion" for all the command words, so it can have descriptive names and yet users do not have to type them all. Many people I've talked to think it's set set...Laruelarum

© 2022 - 2024 — McMap. All rights reserved.