fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / DEBUG
blob6502699a0aded625dba583b1c2b7e69da26a84b1
1 Introduction
2 ============
4 This is a short tutorial on debugging KDE applications. Throughout this
5 tutorial I will use "kedit" as example application.
8 Configuring for debugging
9 =========================
11 You can use -DCMAKE_BUILD_TYPE=Debug with the configure script, if you want to have
12 debug code in your KDE libs. If you have the space and can stand code that's
13 somewhat slower, this is worth it. The extra information really
14 helps debugging and thus bugfixing.
16 On the other hand, -DCMAKE_BUILD_TYPE=None removes all debug messages, leading
17 to a faster and cleaner desktop.
20 Debugging with GDB
21 ==================
23 The recommended version of gdb to use is version 4.95 or higher, older 
24 versions have problems generating proper backtraces.
26 There are three ways to debug an application with gdb:
28 1) You can start the application from within gdb.
29 2) You can attach gdb to an already running application.
30 3) You can run gdb after an application has crashed using a core file.
33 Starting applications from within gdb
34 =====================================
36 To start an application with gdb you can start gdb as follows:
38 > gdb kedit
39 GNU gdb 4.95.0
40 Copyright 2000 Free Software Foundation, Inc.
41 GDB is free software, covered by the GNU General Public License, and you are
42 welcome to change it and/or distribute copies of it under certain conditions.
43 Type "show copying" to see the conditions.
44 There is absolutely no warranty for GDB.  Type "show warranty" for details.
45 This GDB was configured as "i686-pc-linux-gnu"...
46 (gdb) 
48 You can now set the command line arguments that you want to pass to kedit with 
49 the gdb command "set args":
51 (gdb) set args myfile.txt
52 (gdb) 
54 gdb has loaded the kedit executable on startup but it hasn't loaded any of 
55 the libraries yet. This means that you can set any breakpoints in the 
56 libraries yet. The easiest way to do that is to set a breakpoint in the
57 first line of main and then start the program:
59 (gdb) break main
60 Breakpoint 1 at 0x8048741: file /d/kde/build/4/kdeutils/kedit/kedit_dummy.cpp, line 3
61 (gdb) run
62 Starting program: /opt/kde/bin/kedit myfile.txt
64 Breakpoint 1, main (argc=1, argv=0xbfa798b4) at /d/kde/build/4/kdeutils/kedit/kedit_dummy.cpp:3
65 3       int main(int argc, char* argv[]) { return kdemain(argc,argv); }
66 (gdb)  
68 You can now set breakpoints everywhere. For example lets set a breakpoint 
69 in the KApplication constructor. Remember to use quotes before C++ method names:
70 (gdb) break 'KApplication::init()'
71 Breakpoint 2 at 0xb7a5f86d: file /d/kde/src/4/kdelibs/kdeui/kernel/kapplication.cpp, line 488.
72 (gdb) 
74 We can now continue the execution of kedit. Execution will stop when it hits
75 a breakpoint of when the program exits. In this case execution will stop
76 in the first line of the KApplication constructor:
78 (gdb) continue
79 Continuing.
80 Qt: gdb: -nograb added to command-line options.
81          Use the -dograb option to enforce grabbing.
83 Breakpoint 2, KApplication::init (this=0xbfa797b4) at /d/kde/src/4/kdelibs/kdeui/kernel/kapplication.cpp:488
84 488       if ((getuid() != geteuid()) ||
87 Attaching gdb to already running applications
88 =============================================
90 Sometimes it is not practical to start an application from within gdb.
91 E.g. in those cases where you didn't know the application was about to
92 crash :-) When you get the friendly DrKonqi dialog informing you about 
93 a crash you are just in time to start your debugger.
95 First lets attach gdb to an application that hasn't crashed (yet).
97 You start with finding the process of the application with e.g. "ps -aux":
99 > ps -aux | grep kedit 
100 bastian  21570 15.1  6.8 13740 8800 pts/6    S    15:34   0:01 kedit
101 bastian  21582  0.0  0.3  1132  412 pts/6    R    15:34   0:00 grep kedit
103 From this you learn that kedit has process id 21570. Now you can start gdb as
104 follows:
106 > gdb kedit 21570
107 GNU gdb 4.95.0
108 Copyright 2000 Free Software Foundation, Inc.
109 GDB is free software, covered by the GNU General Public License, and you are
110 welcome to change it and/or distribute copies of it under certain conditions.
111 Type "show copying" to see the conditions.
112 There is absolutely no warranty for GDB.  Type "show warranty" for details.
113 This GDB was configured as "i686-pc-linux-gnu"...
114 /home1/bastian/21570: No such file or directory.
115 Attaching to program: /opt/kde/bin/kedit, Pid 21570
116 Reading symbols from /opt/kde/lib/kedit.so.0...done.
117 Loaded symbols for /opt/kde/lib/kedit.so.0
118 ....
119 Reading symbols from /lib/ld-linux.so.2...done.
120 Loaded symbols for /lib/ld-linux.so.2
121 Reading symbols from /lib/libnss_compat.so.2...done.
122 Loaded symbols for /lib/libnss_compat.so.2
123 Reading symbols from /lib/libnsl.so.1...done.
124 Loaded symbols for /lib/libnsl.so.1
125 0x40c3d88e in __select () from /lib/libc.so.6
126 (gdb)  
128 You will usually end up in the middle of a select() call from the event-loop.
129 This is the place where a KDE application spends most of its time, waiting
130 for things to happen.
132 A backtrace will typically look something like this:
134 (gdb) bt
135 #0  0x40c3d88e in __select () from /lib/libc.so.6
136 #1  0x40a22844 in __DTOR_END__ () at fam.c++:356
137 #2  0x407293bf in QApplication::enter_loop (this=0xbffff6e8)
138     at kernel/qapplication.cpp:2552
139 #3  0x406b1d7b in QApplication::exec (this=0xbffff6e8)
140     at kernel/qapplication_x11.cpp:2217
141 #4  0x4002d500 in main (argc=1, argv=0xbffff854) at kedit.cpp:1662
142 #5  0x40bbba5e in __libc_start_main (main=0x8048568 <main>, argc=1,
143     argv=0xbffff854, init=0x8048514 <_init>, fini=0x80486cc <_fini>,
144     rtld_fini=0x4000aa20 <_dl_fini>, stack_end=0xbffff84c)
145     at ../sysdeps/generic/libc-start.c:92
146 (gdb)
149 Getting core dumps
150 ==================
152 If you want to have a core dump after your application crashes you need to 
153 do two things:
155 1) Disable the KDE crash handler. This can be done either by using the 
156 --nocrashhandler command line option or by setting the KDE_DEBUG environment
157 variable to some value e.g. KDE_DEBUG=true.
159 2) Enable core dump generation by changing the so called 'ulimits' with the
160 following command: 
161         ulimit -c unlimited