Iâve got a set of GDB functions for printing JUCE String, StringArray, and StringPairArray objects in the debug console.
Copy and paste the code below into a file called ~/.gdbinit. Itâll give you three new commands in the debug console: ps, psa, and pspa. For example:
(gdb) ps myString
Hello, world!
(gdb) psa myStringArray
[ foo, bar, baz ]
(gdb) pspa myStringPairArray
{ foo: bar, baz: qux }
The functions below use toUTF8(), which means theyâll work even if youâve set JUCE_STRING_UTF_TYPE to 16 or 32, or if youâre using an old version that uses juce_wchar, etc. Unfortunately, because StringArray doesnât have any way to get a one-liner description without constructing a String to pass in, the functions have to grovel through the member variables instead.
The downside to calling functions is that in some situations you canât call functions (if youâve really hosed the stack, it might even crash gdb). So if you only care about recent versions and default settings, you can replace each instance of â.toUTF8()â with â.textâ in the macros.
define ps
if $argc == 0
help ps
else
p $arg0.toUTF8()
end
end
document ps
Prints juce::String
Syntax: ps <String>
end
define psa
if $argc == 0
help psa
else
set $size = $arg0.strings.numUsed
set $i = 0
printf "["
while $i < $size
if $i != 0
printf ", "
else
printf " "
end
printf "%s", $arg0.strings.data.elements.data[$i].toUTF8()
set $i++
end
printf " ]\n"
end
end
document psa
Prints juce::StringArray
Syntax: psa <StringArray>
end
define pspa
if $argc == 0
help pspa
else
set $size = $arg0.keys.strings.numUsed
set $i = 0
printf "{"
while $i < $size
if $i != 0
printf ", "
else
printf " "
end
printf "%s: ", $arg0.keys.strings.data.elements.data[$i].toUTF8()
printf "%s", $arg0.values.strings.data.elements.data[$i].toUTF8()
set $i++
end
printf " }\n"
end
end
document pspa
Prints juce::StringPairArray
Syntax: pspa <StringPairArray>
end
As an alternative, you can add a little code into your program like this:
String desc(const String &s) { return s; }
String desc(const StringArray &sa) { String s; return sa.joinIntoString(s); }
String desc(const StringPairArray &spa) { return spa.getDescription(); }
// ... any other types you want to be able to print out, the way you want them formatted
Then in your .gdbinit:
define desc
p desc($arg0).toUTF8()
end
You can even define an overload for desc(id), and then replace the built-in po with something that prints not only ObjC objects, but also JUCE (and other) C++ objects.