How clear gdb command screen? - c++

Is it possible to clear the command window of gdb? I mean, is there a command in gdb that do the same (for the command windows) as the clear command in a bash terminal?

Hit Control + L. Works for me in Linux and Mac OS X as well with recent versions of GDB.

Depending on the environment you can try this key combination: Ctrl+L. That is: hold down the Control key, and type the L key.
But this may not work in all environments. You can also try the following: in gdb you can execute shell commands using shell: use this to call out to the shell to clear the terminal:
shell clear

Related

gdb script: How can a script determine if it is invoked under `gdb` or `gdb-multiarch`?

I'd like to define a command which does X under gdb-multiarch, but prints out a helpful message when run under normal gdb. How can my script determine which of the two its run under?
Why? When I start gdb-multiarch, I can bind to a qemu-arm session. When I try that in gdb, I get bizarre errors. It's easy to forget and run gdb (and not -multiarch), and I want to my bind-to-qemu tell me "This must be run under gdb-multiarch".
Your question presumes that there is some difference between gdb and gdb-multiarch, but there doesn't have be any such difference.
Presumably on the OS you are using the gdb and gdb-multiarch are configured differently, with gdb only supporting native architecture, while gdb-multiarch supports cross-architecture debugging.
Presumably what you actually want to detect is that the target-architecture you need (arm ?) is / isn't supported by the current binary.
In the bind-to-qemu user-defined function, you can try to set architecture arm.
If that errors out, the rest of bind-to-qemu should not execute.

How to make gdb emit the commands it is executing in batch mode

Is there a way to call gdb --batch --commands some_gdb_script some_executable and have it emit both the commands it executes, along with the command results? I do not want to install anything other than gdb, such as xdotool etc. It should just work within stock gdb with no extensions.
have it emit both the commands it executes, along with the command results?
See this bug.
Using set trace-commands on almost achieves the desired result.

How to clear gdb screen completely?

I'm using this in ~/.bashrc to clear the terminal with F12:
bind '"\e[24~": "\C-k\C-u echo -ne \047\\0033\\0143\047\n"'
Obviously, that doesn't affect gdb or any other program that is launched by the terminal.
It seems that this clears correctly if typed into gdb prompt:
!echo -ne "\033\0143"
So, how to properly clear the gdb screen, so you can't scroll back at all?
That's up to the terminal emulator, the scrollback is a terminal emulator feature.
This answer over on apple.stackexchange.com says that some terminal emulators implement an extension to the ANSI standard that allows this:
printf '\e[3J'
Where printf is the bash command. So, the equivalent echo in gdb should work, if your terminal emulator implements this.
And using https://stackoverflow.com/a/35801000/4742108 , the final solution is to add to ~/.inputrc:
"\e[24~":'!echo -ne \047\\0033\\0143\047\r'
That works because gdb uses the readline library.

system("history") not working

I've run into a snag, I'm trying to implement a linux shell program of sorts with C++ and many of my commands seem to work, however, when I try to get the history(list all recently executed commands) I get an error of "sh: 1: history: not found" the below line is all that runs in the area, what is the issue?
system("history"); //produces the error above ^
If I do
$ history
from the command line it's fine...why is it not fine in C++?
system executes a program using /bin/sh, but history is a bash builtin.
You might look at the contents of ~/.bash_history instead. (Note (by leemes) .bash_history is only updated after closing a previous bash session, as well as it is not updated by executing a command with system.)
Because it's a bash shell builtin not necessarily accessible through /bin/sh -c` (which may be the bourne shell).

How to automatically run the executable in GDB?

I'd like to have gdb immediately run the executable, as if I'd typed "run"
(motivation: I dislike typing "run").
One way is to pipe the command to gdb like this:
$ echo run | gdb myApp
But the problem with this approach is that you lose interactivity with gdb,
eg. if a breakpoint triggers or myApp crashes, gdb quits.
This method is discussed here.
Looking at the options in --help, I don't see a way to do this, but perhaps I'm missing something.
gdb -ex run ./a.out
If you need to pass arguments to a.out:
gdb -ex run --args ./a.out arg1 arg2 ...
EDIT:
Orion says this doesn't work on Mac OSX.
The -ex flag has been available since GDB-6.4 (released in 2005), but OSX uses Apple's fork of GDB, and the latest XCode for Leopard contains GDB 6.3.50-20050815 (Apple version gdb-967), so you are out of luck.
Building current GDB-7.0.1 release is one possible solution. Just be sure to read this.
I would use a gdb-script:
gdb -x your-script
where your-script contains something like:
file a.out
b main
r
afterwards you have the normal interactive gdb prompt
EDIT:
here is an optimization for the truly lazy:
save the script as .gdbinit in the working directory.
Afterwards you simply run gdb as
gdb
... and gdb automatically loads and executes the content of .gdbinit.
(echo r ; cat) | gdb a.out
The cat allows you keep typing after gdb breaks.
start command
This command is another good option:
gdb -ex start --args ./a.out arg1 arg2
It is like run, but also sets a temporary breakpoint at main and stops there.
This temporary breakpoint is deactivated once it is hit.
starti
There is also a related starti which starts the program and stops at the very first instruction instead, see also: Stopping at the first machine code instruction in GDB
Great when you are doing some low level stuff.
gdb -x <(echo run) --args $program $args