用lldb打印带有换行符的字符串 [英] Print string with newlines with lldb
问题描述
我想使用lldb打印一个字符串,无论是const char*
还是std::string
,以便于人类阅读.最重要的是,\n
将被打印为换行符.有谁知道如何做到这一点?我在
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.
我知道您可以发出print myString
命令来打印字符串,但是它不会设置换行符的格式(至少默认情况下不是这样):
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):
推荐答案
大多数时候,您想查看字符串的文字内容,因此用于打印的默认lldb行为是正确的.但是,具有格式选项以与标准库处理字符串相同的方式呈现"输出将很有用.基本上,这就是gdb"printf"命令.请向lldb.llvm.org错误报告者提交错误,以要求这样做.
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.
就像使用gdb一样,您可以获取标准库来为您呈现文本:
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)
在这种情况下,我将其强制转换为void是因为我不在乎返回值,这使得查看文本变得更加困难.
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.
这篇关于用lldb打印带有换行符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!