This is an archive of the discontinued LLVM Phabricator instance.

[lldb][DataFormatters] Support pretty printing std::string when built with -funsigned-char.
ClosedPublic

Authored by rupprecht on Nov 20 2019, 4:02 PM.

Details

Summary

When built w/ -funsigned-char, std::string becomes equivalent to std::basic_string<unsigned char>, causing these formatters to not match. This patch adds overloads for both libstdc++ and libc++ string formatters that accepts unsigned char.

Motivated by the following example:

$ cat pretty_print.cc

template <typename T>
void print_val(T s) {
  std::cerr << s << '\n';  // Set a breakpoint here!
}

int main() {
  std::string val = "hello";
  print_val(val);
  return 0;
}
$ clang++ -stdlib=libc++ -funsigned-char -fstandalone-debug -g pretty_print.cc
$ lldb ./a.out -b -o 'b pretty_print.cc:6' -o r -o 'fr v'
...
(lldb) fr v
(std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> >) s = {
  __r_ = {
    std::__1::__compressed_pair_elem<std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> >::__rep, 0, false> = {
      __value_ = {
         = {
          __l = (__cap_ = 122511465736202, __size_ = 0, __data_ = 0x0000000000000000)
          __s = {
             = (__size_ = '\n', __lx = '\n')
            __data_ = {
              [0] = 'h'
              [1] = 'e'
              [2] = 'l'
              [3] = 'l'
              [4] = 'o'
              [5] = '\0'
...

Diff Detail

Event Timeline

rupprecht created this revision.Nov 20 2019, 4:02 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 20 2019, 4:02 PM

Build result: pass - 60200 tests passed, 0 failed and 732 were skipped.
Log files: console-log.txt, CMakeCache.txt

labath accepted this revision.Nov 21 2019, 12:44 AM

Adding a basic_string<unsigned char> formatter seems reasonable, though I have to say that I was very surprised that adding -funsinged-char should cause the type name of the variable to come out different. I tried building the example program with -funsigned-char and without, and literally the only difference was the value of the DW_AT_encoding attribute on the "char" type -- everything else (including the names of all types) remained the same.

Overall, I am not sure that support for non-native char signedness is really sound in lldb. I wouldn't be surprised if we run into other problems with these kinds of binaries. The first thing that comes to mind is expression evaluation not being able to find the correct function (because it's searching for the wrong char), or it selecting the wrong overload. Unfortunately, making it "sound" could be tricky because of the impedance mismatch between what DWARF provides vs. what clang expects.

This revision is now accepted and ready to land.Nov 21 2019, 12:44 AM
This revision was automatically updated to reflect the committed changes.