How to use valgrind in xterm with gdb on Linux (redhat) ?
The command open a xterm terminal, which disappears immediately.
xterm -e gdb valgrind --tool=drd --read-var-info=yes ./star &
I need it to do debugging for C++ code.
Thanks
If you really want to debug valgrind, the procedure is:
Run xterm -e gdb valgrind &
In the xterm, set up your breakpoints and such
Start the program with run --tool=drd --read-var-info=yes ./star
This should work:
xterm -e gdb --args valgrind --tool=drd --read-var-info=yes ./star &
assuming you want to debug valgrind itself (which seems unlikely, given the question).
If what you really want to do is debug ./star, while it is running under DRD, that used to be pretty much impossible, but became possible with recent valgrind versions, which have embedded gdbserver.
To do that, you'll want two xterms:
xterm -e valgrind --tool=drd --read-var-info=yes --vgdb-error=1 ./star &
xterm -e gdb -ex 'file ./star' -ex 'target remote | /path/to/vgdb' &
Related
I'm making a college assignement where I need to add some modules to a simple SO (EPOS) and to do that I need to be able to compile it using a cross-compiler and also debug it using gdb.
I'm having success on compiling it, and also on running it o QEMU, but when it comes to debugging it, this error shows up:
qemu-system-riscv32 -machine virt -cpu rv32 -smp 1 -gdb tcp::1235 -S -m 0x00020000k -serial mon:stdio -bios none -nographic -no-reboot -device loader,file=hello.img,addr=0x80100000,force-raw=on -kernel hello.img | tee hello.out &
sh -e /home/felipe/UFSC/SO-II/rv32/bin/riscv32-unknown-linux-gnu-gdb -ex "target remote:1235" -ex "set confirm off" -ex "add-symbol-file /home/felipe/UFSC/SO-II/epos/app/hello/hello 0x800000a0"
/home/felipe/UFSC/SO-II/rv32/bin/riscv32-unknown-linux-gnu-gdb: 1: ELF: not found
my professor asked me to test these commands by themselves, and I did. This one:
qemu-system-riscv32 -machine virt -cpu rv32 -smp 1 -gdb tcp::1235 -S -m 0x00020000k -serial mon:stdio -bios none -nographic -no-reboot -device loader,file=hello.img,addr=0x80100000,force-raw=on -kernel hello.img
works fine. But this one:
sh -e /home/felipe/UFSC/SO-II/rv32/bin/riscv32-unknown-linux-gnu-gdb -ex "target remote:1235" -ex "set confirm off" -ex "add-symbol-file /home/felipe/UFSC/SO-II/epos/app/hello/hello 0x800000a0"
is the one responsible for the returning error:
/home/felipe/UFSC/SO-II/rv32/bin/riscv32-unknown-linux-gnu-gdb: 1: ELF: not found
(after "ELF" there are 5 squares, like those that show when you receive and emoji you don't have)
I tested gdb with a simple C program and it worked, so I know that QEMU nor gdb are the problem here.
Its my first time working with this type of development in C/C++ and using QEMU and GDB. I'm not sure if there is anything else I need to share with you regarding code besides the repo link above. If I'm missing something, let me know.
Also, I'm working on WSL2 (using Ubuntu 20.04) but "pure" ubuntu answers are also helpful.
Thank you.
EDIT: after posting I realized that the mentioned squares after ELF are not showing here. So I'm posting a print screen of it:
terminal error
I'm trying to run my program TestApc via gdb and get useful data when my program crash with back trace command ("bt" command).
I wrote a bash script , which look like:
#!/bin/bash
gdb -ex=r --args ./TestApc
When the program crash, I can't type the "bt" command (because I get out from the gdb console)
Is there a way to stay on gdb after the crash ?
I am doing degug for MPI C++ on Linux with GDB.
I cannot use the following command:
xterm -e gdb mpirun -np 1 ./myApplication
to open a window for the executable program ./myApplication: the xterm terminal appears and then disappears immediately.
Why does this happen?
I can open an xterm with:
xterm or xterm -e gdb.
Any help is really appreciated.
#chatan almost got it right.
If you want to invoke gdb on a program while passing arguments to that program, you need to use gdb's --args option. For example (I don't have mpirun, so I'll use /bin/sleep):
$ gdb --args /bin/echo hello
[...]
Reading symbols from /bin/echo...(no debugging symbols found)...done.
(gdb) run
Starting program: /bin/echo hello
hello
Program exited normally.
gdb doesn't automatically start running the program; it waits for input.
Without the --args option, gdb takes -np as a gdb option, not as an argument to mpirun. Since gdb doesn't have a -np option, it terminates with an error message:
$ gdb mpirun -np 1 ./myApplication
gdb: unrecognized option '-np'
Use `gdb --help' for a complete list of options.
And when you run xterm -e gdb mpirun -np 1 ./myApplication, xterm runs, it invokes gdb, gdb terminates with an error message, and xterm terminates before you get a chance to see the message.
So this should do the trick:
xterm -e gdb --args mpirun -np 1 ./myApplication
Of course you'll still have to type the run command within gdb to invoke mpirun. (If you're using gdb, you probably already know that.)
For future reference, if you have problems running a program under xterm -e, try running it by itself.
Your command is not going to work the way you expect it to anyway. gdb will ignore the arguments after 'mpirun'. And a naked mpirun command, without any arguments, is going to immediately exit (just try running mpirun by hand in a terminal). Since your xterm was started to execute that one command, it disappears after that process is finished.
What you need to do is, open an xterm. Then run "gdb mpirun" command.
You should end up in gdb command prompt. At this prompt, you need to issue the following command:
(gdb) run -np 1 ./myApplication
Now your application should be running inside gdb.
I'm writing a script which starts gdb in xterm, and I would like to start my program with the output redirected to a file.
My command is something like this:
xterm -e gdb --args myprog --myargs > /tmp/file
How do I indicate which command the redirection applies to? It could apply to xterm, gdb, or myprog.
I tried using quotes:
xterm -e gdb --args "myprog --myargs > /tmp/file"
But then gdb tried to open a file with name "myprog --myargs > /tmp/file". Any help would be appreciated.
Thanks!
For a reasonably recent version of GDB, the following works:
gdb -q -ex 'set args foo > /tmp/foo.out' -ex run -ex quit /bin/echo
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