Gcc/gdb

Documentation
Debugging with GDB - https://www.sourceware.org/gdb/onlinedocs/gdb.html

Pass Commandline Arguments
Run command with arguments: gdb -ex=r --args myprogram arg1 arg2

You can run gdb with --args parameter gdb --args executablename arg1 arg2 arg3

If you want it to run automatically, place some commands in a file (e.g. 'run') and give it as argument: -x /tmp/cmds. Optionally you can run with -batch mode. gdb -batch -x /tmp/cmds --args executablename arg1 arg2 arg3

Interactive: gdb ./a.out (gdb) r arg1 arg2 arg3

gdb (gdb) file executable (gdb) r arg1 arg2 arg3

Pull in with scripts: gdb prog < file

Backtrace
Print a backtrace of the entire stack: one line per frame for all frames in the stack. backtrace bt

References:
 * Backtrace - Debugging with GDB - https://sourceware.org/gdb/onlinedocs/gdb/Backtrace.html

---

Print stack trace of a core file without needing to enter gdb interactively gdb --batch --quiet -ex "thread apply all bt full" -ex "quit" ${exe} ${corefile}

This will print stack trace of a core file without needing to enter gdb interactively

This does almost the same thing as the original, but it runs the full backtrace for _all_ the threads, which is pretty important when reporting a crash for a multithreaded software, since more often than not, the signal handler is executed in a different thread than the crash happened.

-

Alternatives:

Run a program transparently, but print a stack trace if it fails gdb -batch -ex "run" -ex "bt" ${my_program} 2>&1 | grep -v ^"No stack."$

For automated unit tests I wanted my program to run normally, but if it crashed, to add a stack trace to the output log. I came up with this command so I wouldn't have to mess around with core files.

The one downside is that it does smoosh your program's stderr and stdout together.

-

Print stack trace of a core file without needing to enter gdb interactively alias gdbbt="gdb -q -n -ex bt -batch"

The pstack command prints a stack trace of running processes without needing to attach a debugger, but what about core files? The answer, of course, is to use this command. Usage: gdbbt program corefile

Ubuntu - Cannot find file ‘../sysdeps/unix/syscall-template.S’
Error: Cannot find file ‘../sysdeps/unix/syscall-template.S’ ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.

Fix sudo apt-get dpkg-dev
 * 1) dependency

sudo apt-get source libc6 cd eglibc-2.19 git clone .. # into the eglibc folder
 * 1) dumps eglibc in current folder

Alt Fix: cd .. sudo apt-get source libc6 ln -s eglibc-2.19/sysdeps sysdeps

Square Goldfish » Debugging in Ubuntu 12.10 – missing file syscall-template.S - http://www.squaregoldfish.co.uk/2013/01/06/debugging-in-ubuntu-12-10-missing-file-syscall-template-s/