Search code examples
cgdblldb

lldb debugger shows type-qualifier const in string literal type. Is this a bug in lldb?


In C, the type of a string literal is char [].

gdb debugger:

(gdb) ptype ""
type = char [1]
(gdb) ptype "abc"
type = char [4]

lldb debugger:

(lldb) p ""
(const char [1]) $0 = ""
(lldb) p "abc"
(const char [4]) $1 = "abc"

The gdb debugger shows type of string literal as char [N] (which is as per language standard) but lldb debugger shows const type-qualifier in string literal type - const char [N].
Is this a bug in lldb debugger? Or, am I missing something?


Solution

  • Its because the lldb debugger uses ISO C++ rules/specifications while evaluating an expression given to the print command and in C++ the type of ordinary string literal is const char[N], where N is the size of the string including the null terminator.

    Proof of lldb debugger uses ISO C++ rules/specifications:

    Assume a C program has a variable definition:

        int x;
    

    While debugging this program using lldb debugger, if I do:

    (lldb) p sizeof (*(void *)&x)
    error: expression failed to parse:
    warning: <user expression 4>:1:9: ISO C++ does not allow indirection on operand of type 'void *'
    sizeof (*(void *)&x)
            ^~~~~~~~~~~
    error: <user expression 4>:1:8: invalid application of 'sizeof' to an incomplete type 'void'
    sizeof (*(void *)&x)
           ^~~~~~~~~~~~~
    

    In the error, it says ISO C++ ...
    This proves that lldb debugger uses C++ language rules/specifications to evaluate an expression given to print command. Hence, lldb debugger shows type const char [4] in the output of command p "abc".


    Additional:

    In gdb debugger, we can set the current source language using set language command:

    (gdb) show language
    The current source language is "auto; currently c".
    
    (gdb) set language c++
    Warning: the current language does not match this frame.
    
    (gdb) show language
    The current source language is "c++".
    Warning: the current language does not match this frame.
    
    (gdb) ptype "abc"
    type = char [4]
    

    Even after setting language to c++ the output showing type as char [4]. It may be because set language command only set the current source language but while evaluating the expressions given to print command, it might be using C language rules.

    I did not find anything similar in lldb debugger to set/reset source language during debugging.