Samuel Jacob's Weblog

Just another technical blog

LLDB Backtrace formatting

without comments

lldb can be configured to print backtraces with syntax highlighting. Here is how to setup lldb to do that

Consider the following source level debugging session,
[codegroup]
[c tab=’source’]
$ cat test.c
static void crash_me()
{
char *c = 0;
*c = 0;
}

static void recursive_call(int value)
{
if (value == 0) {
crash_me();
}
recursive_call(value – 1);
}

int main(int argc, char argv[])
{
recursive_call(argc);
}
[/c]
[shell tab=’commands’]
$ gcc -g3 -O3 test.c
$ lldb a.out
(lldb) run 0 1 2 3 4 5
[/shell]
[/codegroup]

Without color syntax the backtrace would look like the following.

Normal backtrace with out any makeup

Normal backtrace with out any makeup

Since lldb supports ANSI escape sequence, the escape sequences can be used to color the backtrace output which makes output more readable. Here is the link to official lldb page describing this feature – http://lldb.llvm.org/formats.html.

Here is my backtrace setting and example

[shell]
(lldb) settings set frame-format “frame #${frame.index}: ${frame.pc}{ \x1b\x5b36m${module.file.basename}\x1b\x5b39m{` \x1b\x5b33m${function.name-with-args} \x1b\x5b39m${function.pc-offset}}}{ at ${line.file.basename}:${line.number}}\n”
[/shell]

Backtrace with color

Backtrace with color

Similarly thread format cant be colorized so that ‘thread list‘ would look neat.

[shell]
(lldb) settings set thread-format “\x1b\x5b42;1mthread #${thread.index}: tid = ${thread.id}{, ${frame.pc}}{ \x1b\x5b31m${module.file.basename}\x1b\x5b39m{`${function.name-with-args}${function.pc-offset}}}{ at ${line.file.basename}:${line.number}}{, name = ‘\x1b\x5b34m${thread.name}}\x1b\x5b39m{, queue = ‘${thread.queue}}{, stop reason = ${thread.stop-reason}}{\nReturn value: ${thread.return-value}}\x1b\x5b0m\n”
[/shell]

Reference to ANSI escape sequence – http://ascii-table.com/ansi-escape-sequences.php

Written by samueldotj

July 22nd, 2013 at 9:20 pm

Posted in C,Debugger,lldb