Penguin

Differences between version 8 and predecessor to the previous major change of DeBugging.

Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History

Newer page: version 8 Last edited on Tuesday, October 28, 2003 11:22:58 am by JohnMcPherson Revert
Older page: version 5 Last edited on Sunday, September 7, 2003 10:43:46 am by DanielLawson Revert
@@ -7,27 +7,30 @@
 # Load your program in gdb: "gdb ./''programname''" 
 # type "run" at the prompt to run the program. 
 # when the program crashes you should be able to type "bt full" to get a full backtrace of what the program was doing at the time. 
  
-useful gdb(1) commands: 
+!! useful gdb(1) commands: 
 ;bt full: give a complete backtrace of a program. If a program crashes __this__ is what the programmer will want from you. 
 ;print: This lets you print out various expressions, eg: "print node" "print *node" "print node->key" "print node->next->key" etc. 
 ;break: This lets you set breakpoints, eg: "break main" 
 ;run: run a program up until a break point. 
 ;step: step over a function call 
 ;next: step into a function call 
 ;frame: change which frame you are working on. eg: "frame 1" will change the scope to frame 1. 
  
-!Other useful debugging tricks and traps:  
-strace(1) lets you see what a program is doing in a corse kind of way, if you think strace(1) is too quiet, perhaps ltrace(1) is for you. for the bsdites amongst us, I believe these are called struss(1) and sotrace(1). The command for this is: 
+! !Other useful debugging tricks and traps:  
+!strace  
+strace(1) lets you see what a program is doing in a coarse kind of way, if you think strace(1) is too quiet, perhaps ltrace(1) is for you. for the bsdites amongst us, I believe these are called struss(1) and sotrace(1). The command for this is: 
  strace ''programname'' 
 if the program is already running: 
- strace ''pid'' 
+ strace -p ''pid'' 
 will also work. 
  
+!signals  
 If your program hangs, you can press Alt-\ to send it a SIGQUIT and force it to dump core. You can also force them to dump core with the command: 
  kill -QUIT ''programpid'' 
  
+!Core files  
 To create core files you have to remove the ulimit(1) on them. This can be done with the command: 
  ulimit -c unlimited 
 Note, this is for the shell (and all it's children) only. 
  
@@ -35,11 +38,36 @@
  gdb ./''program'' ./''corefile'' 
  
 If you run gdb(1) on your program and it displays the names of the functions but doesn't display their types (eg: what arguments they have or line number information) you probably didn't compile them with -g 
  
+! modifying a running process  
+You can use gdb to attach to a currently running process. For example, to change where its stderr is going:  
+  
+ $ gdb <executable> <process_id>  
+ (gdb) call close(2)  
+ $1 = 0  
+ (gdb) call open("/tmp/prog-debug", 0101)  
+ $2 = 2  
+ (gdb) cont  
+  
+Note that the octal 0101 stands for O_CREAT|O_WRONLY, since gdb will complain about no debugging symbols for resolving those words otherwise. Check with your /usr/include files... the c library with debian testing at least has these definitions in /usr/include/bits/fcntl.h. (0100 + 01).  
+  
+!ddd  
 ddd(1) appears to be a reasonable [GUI] interface to gdb(1) for those that are afraid of CommandLine's. 
  
+!assert  
 use assert(3) everywhere. It's much nicer at finding your bugs closer to where the bug actually hides. 
  
+!Electric Fence  
 Make use of electricfence (libefence) for tracking memory allocation errors. In debian this can be enabled on the fly by setting an LD_PRELOAD variable, like so: 
  
  LD_PRELOAD=libefence.so.0.0 your-buggy-programme 
+  
+! threads  
+Note: When using gdb(1) to debug a threaded program, gdb(1) catches two signals ([SIGPWR] & [SIGXCPU]) which are used internally by pthreads on Linux. Use  
+ (gdb) handle SIGPWR pass nostop noprint  
+ (gdb) handle SIGXCPU pass nostop noprint  
+to stop gdb halting on receiving these signals.  
+  
+!Other useful commands  
+* catchsegv - prints out the backtrace of function calls for a program receiving a [SIGSEGV]  
+* c++filt(1) - reverses the C++ symbol name-mangling that gcc(1) does, so if the program uses c++ then you can read the function names.