Why my gdb prompt shows wrong after I change its color

931 views Asked by At

I change my gdb prompt's color by writing set prompt \033[1;33m(gdb) \033[0m into .gdbinitfile. And I change my gdb prompt's color sucessfully.

But I find that my long command with my parameters will overwrite my prompt after I input a long command without going to newline. Why?

2

There are 2 answers

0
Mark Plotnick On BEST ANSWER

Edit: if your gdb has python scripting enabled, look at @matt's answer to see how to do this using the set extended-prompt command - it's a better solution.

Gdb manages command input by using the readline package. The way to tell readline that a character sequence in a prompt string doesn't actually move the cursor when output to the screen is to surround it with the markers RL_PROMPT_START_IGNORE (currently '\001' in readline's C header file) and RL_PROMPT_END_IGNORE (currently '\002').

Bash has a portable way of expressing this: when it sees "\[" and "\]" in the prompt variable, it will convert them to RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE. Bash does this while it's processing various other escape sequences such as \w to include the current working directory.

Gdb's set prompt command doesn't support "\[" and "\]", but you can put the octal escapes \001 and \002 in your set prompt string (subject to change if readline's authors ever choose to use a different set of markers).

set prompt \001\033[1;33m\002(gdb) \001\033[0m\002
0
matt On

gdb has a command: set extended-prompt that accepts escape sequences described in the gdb.prompt python module. which includes the \[ and \] bashism's for wrapping characters that do not contribute to the prompts length. This of course requires a python enabled gdb to work though.