简体   繁体   English

使用lldb打印带换行符的字符串

[英]Print string with newlines with lldb

I'd like to print a string, either const char* or std::string , using lldb so that it is human readable. 我想使用lldb打印一个字符串, const char*std::string ,这样它就是人类可读的。 Most importantly, \\n 's would be printed as a newline. 最重要的是, \\n将被打印为换行符。 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. 我在这篇文章中尝试了为gdb提供的建议,但它似乎不适用于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): 编辑:我知道您可以发出print myString命令来打印字符串,但是它不会格式化换行符(至少在默认情况下不是这样):

Most of the time you want to see the literal contents of your strings, so the default lldb behavior for print is correct. 大多数情况下,您希望查看字符串的文字内容,因此打印的默认lldb行为是正确的。 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. 这基本上就是gdb“printf”命令。 Please file a bug with the lldb.llvm.org bug reporter asking for this. 请向lldb.llvm.org错误记者提出错误请求。

Just like with gdb, you can get the standard library to render the text for you: 就像使用gdb一样,您可以获得标准库来为您呈现文本:

(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. 在这种情况下我将它转换为void,因为我不关心返回值,这使得查看文本变得更加困难。

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. 但是,只有在调试器中附加,而不是运行程序时才会发生这种情况。

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

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