FAQ
Contents
- Where is the main GDB project page?
- Is there online documentation available?
- How do I disable the "Type <return> to continue, or q <return> to quit" pagination prompt in GDB?
- How do I redirect output from GDB to a file?
- How can I get GDB to remember command history across sessions?
- GDB reports a nonsensical return value from an inferior function call. What's going on?
- How do I load/unload a shared library in GDB?
- How to show the current instruction when single-stepping instructions?
- GDB reports "Cannot find user-level thread for LWP 23957: generic error", how do I fix this?
- GDB does not see any threads besides the thread in which crash occurred; or SIGTRAP kills my program when I set a breakpoint.
- GDB 6.8 doesn't compile with GCC x.y because of -Werror, what should I do?
- Why doesn't anybody on the GDB IRC channel answer my question about my small distro's GDB, or my custom GDB?
- I've run into a bug in GDB while using XCode. Can you help?
- How do I print an STL container?
- When connecting to gdbserver I get "Remote register badly formatted", "g packet reply to long" etc.
- When I try "break 1" or "list" I get: "No symbol table is loaded."
- Ending of the string is truncated with "...", is there a way to get the whole string?
- How to trace every function entry and return?
- Getting an internal error or other error while attaching to processes on GNU/Linux
- Is there a way to set commands to be run on a segfault?
- Is there a way to step into the last call (foo) without stepping into the inner calls (bar, baz)? "foo (bar (), baz ());"
- GDB is printing "optimized out" when I try to print a variable
- How do I abandon a hand-called function?
1. Where is the main GDB project page?
2. Is there online documentation available?
- Note: This is generated from the main development tree, and may describe features not in the release you are using.
3. How do I disable the "Type <return> to continue, or q <return> to quit" pagination prompt in GDB?
Either with set height 0 or set pagination off
4. How do I redirect output from GDB to a file?
See help set logging
5. How can I get GDB to remember command history across sessions?
(gdb) set history save on (gdb) set history filename ~/.gdb-history (gdb) set history size 1000
- Note: This may become the default, but for older GDBs do the above.
6. GDB reports a nonsensical return value from an inferior function call. What's going on?
- GDB doesn't know the return type nor the type of the arguments for that function call, because there's no debug information available for it. Either provide debuginfo for the program or library which contains the function, or cast the function to a function pointer of the appropriate signature.
For example, to call fabs, which takes a double and returns a double, use:
(gdb) print ((double (*) (double)) fabs) ( -1.25 )
7. How do I load/unload a shared library in GDB?
- GDB doesn't manipulate shared libraries. This is done by the operating system's dynamic linker running in the inferior. GDB just obtains the list of shared libraries from it, and works with that.
If one wants to manually load a shared library into the inferior one can manually call dlopen and dlsym, or the equivalent in your system, if there is one. However you have to know whether the inferior is stopped at a safe point to do so; often that's the case, but caveat emptor.
(gdb) set $dlopen = (void*(*)(char *, int)) dlopen (gdb) set $my_lib = $dlopen ("/tmp/mylib.so", 1) // 1 == RTLD_LAZY (gdb) set $dlsym = (void*(*)(void*, char *)) dlsym (gdb) set $foo = $dlsym ($my_lib, "foo") // int foo () { return 42; } (gdb) p $foo () $1 = 42 (gdb) set $dlclose = (int(*)(void*)) dlclose (gdb) call $dlclose ($my_lib)
See LoadingCodeIntoActiveSession for a more complete example.
8. How to show the current instruction when single-stepping instructions?
(gdb) display/i $pc
9. GDB reports "Cannot find user-level thread for LWP 23957: generic error", how do I fix this?
- There are several common causes:
You have a mismatch between libthread_db.so.1 and libpthread.so.0 (this most often happens when you have multiple installations of glibc, or when you debug a program on remote target, and host and target have different glibc versions).
- You are using 64-bit debugger to debug 32-bit program, and your kernel has a 32-bit ptrace emulation bug. FIXME: add reference to specific kernel fix.
This has also been known to happen when one of DOSEMU's signal handlers is invoked from DPMI context, where the $gs register has a value different from what GDB and/or libthread_db.so.0 expect; SamuelBronson found running the program under gdbserver to alleviate the problem, at least with version 6.8.50.20090620-cvs on i386.
10. GDB does not see any threads besides the thread in which crash occurred; or SIGTRAP kills my program when I set a breakpoint.
- This frequently happens on Linux, especially on embedded targets. There are two common causes:
you are using glibc, and you have stripped libpthread.so.0
mismatch between libpthread.so.0 and libthread_db.so.1
GDB itself does not know how to decode "thread control blocks" maintained by glibc and considered to be glibc private implementation detail. It uses libthread_db.so.1 (part of glibc) to help it do so. Therefore, libthread_db.so.1 and libpthread.so.0 must match in version and compilation flags. In addition, libthread_db.so.1 requires certain non-global symbols to be present in libpthread.so.0. Solution: use strip --strip-debug libpthread.so.0 instead of strip libpthread.so.0.
11. GDB 6.8 doesn't compile with GCC x.y because of -Werror, what should I do?
- GDB 6.8 was released with -Werror configured in by default. You can disable it by configuring with:
$ /path/to/gdb-6.8/configure --disable-werror
12. Why doesn't anybody on the GDB IRC channel answer my question about my small distro's GDB, or my custom GDB?
- We can help with FSF's original GDB, and also with the GDBs shipped by the biggest distros. if you are lucky, somebody might know some peculiarities about some small distro's GDB.
13. I've run into a bug in GDB while using XCode. Can you help?
- Apple has their own fork of GDB, and they are no longer pulling changes from the FSF GDB. Any bugs in this fork should be reported to Apple; there's nothing we can do about them. The FSF GDB has recently been ported to MacOS; however, it is believed that it will not work in XCode.
14. How do I print an STL container?
See STLSupport.
15. When connecting to gdbserver I get "Remote register badly formatted", "g packet reply to long" etc.
Your gdb 32bit/64bit architecture setting may not match that of gdbserver. Try to load the executable by file first. You may also check set/show architecture.
16. When I try "break 1" or "list" I get: "No symbol table is loaded."
Compile the debugged program with gcc option -g.
17. Ending of the string is truncated with "...", is there a way to get the whole string?
Use set print elements -1.
18. How to trace every function entry and return?
With recompilation try itrace.c. Without recompilation using SystemTap try a SystemTap script. Without recompilation using GDB try trace.pl GDB commands generator. Otherwise just for breakpoint on each function entry you can use GDB rbreak. With recompilation one can also put breakpoint on __cyg_profile_func_enter and __cyg_profile_func_exit as done in itrace.c.
19. Getting an internal error or other error while attaching to processes on GNU/Linux
Try setenforce 0 (SELinux) or echo 0 >/proc/sys/kernel/yama/ptrace_scope (ptrace scope) to disable system security protections.
20. Is there a way to set commands to be run on a segfault?
Without GDB you can setup (with Linux kernel) /proc/sys/kernel/core_pattern. See man 5 core. It is used by bugreporting tools like ABRT or Apport.
21. Is there a way to step into the last call (foo) without stepping into the inner calls (bar, baz)? "foo (bar (), baz ());"
Such native feature is missing in GDB (DW_TAG_GNU_call_site should make it possible now).
- There are several workarounds though:
Write a macro using record, next, reverse-step, reverse-finish, step, record stop if the inferior functions do not run for too long.
Get ignorefunc.cmd and use ignorefunc bar and ignorefunc baz before doing step.
Use the skip command. Do skip bar and skip baz before doing step.
22. GDB is printing "optimized out" when I try to print a variable
- This commonly happens when debugging optimized code. The compiler is far more interested in generating optimal code than in generating code for which GDB can reconstruct a variable's value at an arbitrary point in the program. And that's A Good Thing for high levels of optimization of course. When this happens the compiler may generate debug information for a variable, but will leave out telling GDB how to fetch the variable's value (because it's either impossible or too difficult). GDB recognizes this and then prints such variables as "optimized out".
23. How do I abandon a hand-called function?
- Set the frame to the entry for the hand-called function (marked by "function called from gdb")
and then use the return command.
(gdb) l hand 1 2 int 3 hand () 4 { 5 return *(int*) 0; 6 } 7 8 int 9 main () 10 { (gdb) p hand() Program received signal SIGSEGV, Segmentation fault. 0x00000000004006d9 in hand () at hand-segv.c:5 5 return *(int*) 0; The program being debugged was signaled while in a function called from GDB. GDB remains in the frame where the signal was received. To change this behavior use "set unwindonsignal on". Evaluation of the expression containing the function (hand) will be abandoned. When the function is done executing, GDB will silently stop. (gdb) bt #0 0x00000000004006d9 in hand () at hand-segv.c:5 #1 <function called from gdb> #2 main () at hand-segv.c:11 (gdb) f 1 #1 <function called from gdb> (gdb) return Make selected stack frame return now? (y or n) y #0 main () at hand-segv.c:11 11 return hand (); (gdb) bt #0 main () at hand-segv.c:11