1 # Develop Icinga 2 <a id="development"></a>
3 This chapter provides hints on Icinga 2 development
4 especially for debugging purposes.
8 > If you are planning to build your own development environment,
9 > please consult the `INSTALL.md` file from the source tree.
11 ## Debug Requirements <a id="debug-requirements"></a>
13 Make sure that the debug symbols are available for Icinga 2.
14 The Icinga 2 packages provide a debug package which must be
15 installed separately for all involved binaries, like `icinga2-bin`
16 or `icinga2-ido-mysql`.
20 # apt-get install icinga2-dbg
24 # yum install icinga2-debuginfo
28 # zypper install icinga2-bin-debuginfo icinga2-ido-mysql-debuginfo
31 Furthermore, you may also have to install debug symbols for Boost and your C library.
33 If you're building your own binaries, you should use the `-DCMAKE_BUILD_TYPE=Debug` cmake
34 build flag for debug builds.
37 ## GDB <a id="development-debug-gdb"></a>
54 Install the `boost`, `python` and `icinga2` pretty printers. Absolute paths are required,
55 so please make sure to update the installation paths accordingly (`pwd`).
57 $ mkdir -p ~/.gdb_printers && cd ~/.gdb_printers
59 Boost Pretty Printers compatible with Python 3:
61 $ git clone https://github.com/mateidavid/Boost-Pretty-Printer.git && cd Boost-Pretty-Printer
62 $ git checkout python-3
64 /home/michi/.gdb_printers/Boost-Pretty-Printer
66 Python Pretty Printers:
69 $ svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
71 Icinga 2 Pretty Printers:
73 $ mkdir -p ~/.gdb_printers/icinga2 && cd ~/.gdb_printers/icinga2
74 $ wget https://raw.githubusercontent.com/Icinga/icinga2/master/tools/debug/gdb/icingadbg.py
76 Now you'll need to modify/setup your `~/.gdbinit` configuration file.
77 You can download the one from Icinga 2 and modify all paths.
81 $ wget https://raw.githubusercontent.com/Icinga/icinga2/master/tools/debug/gdb/gdbinit -O ~/.gdbinit
88 sys.path.insert(0, '/home/michi/.gdb_printers/icinga2')
89 from icingadbg import register_icinga_printers
90 register_icinga_printers()
95 sys.path.insert(0, '/home/michi/.gdb_printers/python')
96 from libstdcxx.v6.printers import register_libstdcxx_printers
98 register_libstdcxx_printers(None)
105 sys.path.insert(0, '/home/michi/.gdb_printers/Boost-Pretty-Printer')
107 boost_print.register_printers()
111 If you are getting the following error when running gdb, the `libstdcxx`
112 printers are already preloaded in your environment and you can remove
113 the duplicate import in your `~/.gdbinit` file.
115 RuntimeError: pretty-printer already registered: libstdc++-v6
117 ### GDB Run <a id="development-debug-gdb-run"></a>
119 Call GDB with the binary (`/usr/sbin/icinga2` is a wrapper script calling
120 `/usr/lib64/icinga2/sbin/icinga2` since 2.4) and all arguments and run it in foreground.
122 # gdb --args /usr/lib64/icinga2/sbin/icinga2 daemon -x debug --no-stack-rlimit
124 The exact path to the Icinga 2 binary differs on each distribution. On Ubuntu
125 it is installed into `/usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2` on 64-bit systems
130 > If gdb tells you it's missing debug symbols, quit gdb and install
131 > them: `Missing separate debuginfos, use: debuginfo-install ...`
137 Kill the running application.
141 Continue after breakpoint.
145 ### GDB Core Dump <a id="development-debug-gdb-coredump"></a>
147 Either attach to the running process using `gdb -p PID` or start
151 (gdb) generate-core-file
153 ### GDB Backtrace <a id="development-debug-gdb-backtrace"></a>
155 If Icinga 2 aborted its operation abnormally, generate a backtrace.
158 (gdb) thread apply all bt full
160 If gdb stops at a SIGPIPE signal please disable the signal before
163 (gdb) handle SIGPIPE nostop noprint pass
166 If you create a [bug report](https://www.icinga.com/community/get-involved/),
167 make sure to attach as much detail as possible.
169 ### GDB Backtrace from Running Process <a id="development-debug-gdb-backtrace-running"></a>
171 If Icinga 2 is still running, generate a full backtrace from the running
172 process and store it into a new file (e.g. for debugging dead locks):
174 # gdb -p $(pidof icinga2) -batch -ex "thread apply all bt full" -ex "detach" -ex "q" > gdb_bt.log
176 ### GDB Backtrace Stepping <a id="development-debug-gdb-backtrace-stepping"></a>
178 Identifying the problem may require stepping into the backtrace, analysing
179 the current scope, attributes, and possible unmet requirements. `p` prints
180 the value of the selected variable or function call result.
185 (gdb) p checkable.px->m_Name
188 ### GDB Breakpoints <a id="development-debug-gdb-breakpoint"></a>
190 To set a breakpoint to a specific function call, or file specific line.
192 (gdb) b checkable.cpp:125
193 (gdb) b icinga::Checkable::SetEnablePerfdata
195 GDB will ask about loading the required symbols later, select `yes` instead
198 Then run Icinga 2 until it reaches the first breakpoint. Continue with `c`
204 If you want to delete all breakpoints, use `d` and select `yes`.
210 > When debugging exceptions, set your breakpoint like this: `b __cxa_throw`.
219 #11 0x00007ffff7cbf9ff in icinga::Utility::GlobRecursive(icinga::String const&, icinga::String const&, boost::function<void (icinga::String const&)> const&, int) (path=..., pattern=..., callback=..., type=1)
220 at /home/michi/coding/icinga/icinga2/lib/base/utility.cpp:609
224 605 #endif /* _WIN32 */
226 607 std::sort(files.begin(), files.end());
227 608 BOOST_FOREACH(const String& cpath, files) {
231 612 std::sort(dirs.begin(), dirs.end());
232 613 BOOST_FOREACH(const String& cpath, dirs) {
234 $3 = std::vector of length 11, capacity 16 = {{static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/agent.conf"}, {static NPos = 18446744073709551615,
235 m_Data = "/etc/icinga2/conf.d/commands.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/downtimes.conf"}, {static NPos = 18446744073709551615,
236 m_Data = "/etc/icinga2/conf.d/groups.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/notifications.conf"}, {static NPos = 18446744073709551615,
237 m_Data = "/etc/icinga2/conf.d/satellite.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/services.conf"}, {static NPos = 18446744073709551615,
238 m_Data = "/etc/icinga2/conf.d/templates.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/test.conf"}, {static NPos = 18446744073709551615,
239 m_Data = "/etc/icinga2/conf.d/timeperiods.conf"}, {static NPos = 18446744073709551615, m_Data = "/etc/icinga2/conf.d/users.conf"}}
242 ## Core Dump <a id="development-debug-core-dump"></a>
244 When the Icinga 2 daemon crashes with a `SIGSEGV` signal
245 a core dump file should be written. This will help
246 developers to analyze and fix the problem.
248 ### Core Dump File Size Limit <a id="development-debug-core-dump-limit"></a>
250 This requires setting the core dump file size to `unlimited`.
254 vim /usr/lib/systemd/system/icinga2.service
260 systemctl daemon-reload
262 systemctl restart icinga2
264 Example for init script:
266 vim /etc/init.d/icinga2
270 service icinga2 restart
272 Verify that the Icinga 2 process core file size limit is set to `unlimited`.
274 cat /proc/`pidof icinga2`/limits
276 Max core file size unlimited unlimited bytes
279 ### Core Dump Kernel Format <a id="development-debug-core-dump-format"></a>
281 The Icinga 2 daemon runs with the SUID bit set. Therefore you need
282 to explicitly enable core dumps for SUID on Linux.
284 sysctl -w fs.suid_dumpable=1
286 Adjust the coredump kernel format and file location on Linux:
288 sysctl -w kernel.core_pattern=/var/lib/cores/core.%e.%p
290 install -m 1777 -d /var/lib/cores
294 sysctl -w kern.corefile=/cores/core.%P
298 ### Core Dump Analysis <a id="development-debug-core-dump-analysis"></a>
300 Once Icinga 2 crashes again a new coredump file will be written. Please
301 attach this file to your bug report in addition to the general details.
303 Simple test case for a `SIGSEGV` simulation with `sleep`:
309 gdb `which sleep` /var/lib/cores/core.sleep.<PID>
311 rm /var/lib/cores/core.sleep.*
315 gdb /usr/lib64/icinga2/sbin/icinga2 core.icinga2.<PID>