How to automatically refresh gdb in tui mode?

John Goofy picture John Goofy · Aug 6, 2016 · Viewed 9.4k times · Source

If I'am debugging files with gdb -tui the source window always becomes messed up. So every time I hit enter I have to immediately type ctrl+L to get rid of this problem, this is how gdb refeshes the window. I am working on tty with gnu screen.

Is there a possibility to automatically refresh gdb in tui mode?
If gdb doesn't have this ability Python could be a solution because gdb is able to source Python files, but I don't know about Python.

This Python snippet works fine in Bash but not inside gdb:

import sys
r = "\033[2J"    # here I try to emulate [ctrl-L]
t = ""
while 1:
    i = sys.stdin.read(1)
    t = t +i
    if i == '\n':
        print(r)

Of course I accept every other language supported by gdb.
Every help is appreciated.

By the way, here is a screencast https://youtu.be/DqiH6Jym1JY that show my problem.

This is the file I used for demonstrating in gdb like the link above show's, mess_up.c

#include <stdio.h>

int main(void){
    //int n = 120;
    int n;
    n = 120;
    char stuff[n+2];

    printf( "Max: %d\n", n );

    printf( "Sizeof int:  %d\n", sizeof(int)  );
    printf( "Sizeof char: %d\n", sizeof(char) );
    printf( "Sizeof n:  %d\n", sizeof n   );
    printf( "Sizeof stuff: %d\n", sizeof stuff  );

    fgets ( stuff , n , stdin );
    printf( "The stuff:\n%s\n", stuff );
    printf( "Sizeof stuff after input = %d\n", sizeof stuff  );

return 0;
}

My actual ncurses version displayed by tic -V is ncurses 5.9.20140118

Answer

hdl picture hdl · Jan 12, 2018

Had the exact same problem. Have you tried GDB user-defined hooks or commands ?

In your ~/.gdbinit or in your session, you can do:

define hook-next
  refresh
end

This will call the refresh command each time you enter the next command or one of its aliases.

Or you can define:

define mynext
  next
  refresh
end

and call mynext instead of next.

Hooks are automatically called whenever a command C is entered and a hook-C exists, that's so cool, I've just discovered that in the docs.

See https://sourceware.org/gdb/current/onlinedocs/gdb/Define.html and https://sourceware.org/gdb/current/onlinedocs/gdb/Hooks.html#Hooks

You can add as many hooks/defines as you want.