How do I print the full value of a long string in gdb?

后端 未结 6 913
栀梦
栀梦 2020-11-28 17:17

I want to print the full length of a C-string in GDB. By default it\'s being abbreviated, how do I force GDB to print the whole string?

6条回答
  •  野性不改
    2020-11-28 17:47

    Using set elements ... isn't always the best way. It would be useful if there were a distinct set string-elements ....

    So, I use these functions in my .gdbinit:

    define pstr
      ptype $arg0._M_dataplus._M_p
      printf "[%d] = %s\n", $arg0._M_string_length, $arg0._M_dataplus._M_p
    end
    
    define pcstr
      ptype $arg0
      printf "[%d] = %s\n", strlen($arg0), $arg0
    end
    

    Caveats:

    • The first is c++ lib dependent as it accesses members of std::string, but is easily adjusted.
    • The second can only be used on a running program as it calls strlen.

提交回复
热议问题