Project

General

Profile

Debugging » History » Version 2

Ole Hansen, 08/27/2022 12:12 PM

1 1 Ole Hansen
h1. Debugging
2 2 Ole Hansen
3
Advanced users may be interested in creating a debug build. To do so, build the analyzer with the CMake build type "Debug":
4
<pre>
5
cmake -B build-debug -S . -DCMAKE_BUILD_TYPE=Debug
6
cmake --build build-debug -j
7
</pre>
8
You might also want to use a debug version of ROOT or install an appropriate debuginfo package (e.g. root-debuginfo on RedHat systems using EPEL).
9
A true debug version of ROOT requires "building ROOT from source":https://root.cern/building-root, again using <code>CMAKE_BUILD_TYPE=Debug</code>, which is beyond the scope of this document. 
10
11
Also, be sure to install glibc-debuginfo and glibc-debuginfo-common to get debug support for STL classes and other standard library features. This is not necessary on macOS, where the relevant bundles come with the required symbol tables included.
12
13
The recommended debugger is <code>gdb</code> on Linux and <code>lldb</code> on newer macOS versions. To avoid problems with macOS's System Integrity Protection when using lldb, it may be helpful to not use /usr/bin/lldb directly, but instead create an alias to the command-line tools version, which isn't a specially-protected executable:
14
<pre>
15
alias lldb='/Library/Developer/CommandLineTools/usr/bin/lldb'
16
</pre>
17
The analyzer may be run directly from the build location under the debugger of your choice:
18
<pre>
19
% ./build-debug/apps/analyzer
20
</pre>
21
In a second terminal, find out the PID of this analyzer process, then attach the debugger, which will stop the analzyer and allow you to configure the debugger, e.g. to set break points, etc. For example, on Linux using <code>gdb</code>:
22
<pre>
23
% ps a | grep analyzer
24
13643 pts/3    S+     0:00 ./build-debug/apps/analyzer
25
% gdb
26
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
27
...
28
(gdb) attach 13643
29
...
30
(gdb) break THaRun::THaRun
31
Breakpoint 1 at 0x7fab342a10a7: THaRun::THaRun. (3 locations)
32
(gdb) continue
33
Continuing.
34
</pre>
35
This sets a breakpoint for all versions of the <code>THaRun</code> constructor. The <code>continue</code> command (shorthand <code>c</code>) resumes execution of the program.
36
37
Go back to the analyzer prompt and create a <code>THaRun</code> object. The process will stop and print information (in the debugger window), allowing you to inspect data, single step through the code, etc.:
38
<pre>
39
analyzer [0] r = new THaRun("/data/raw/run_7211.evio.0")
40
</pre>
41
In the debugger window, you'll see
42
<pre>
43
Breakpoint 1, THaRun::THaRun (this=0x503bb20,
44
    fname=0x7fab188f002c "/data/raw/run_7211.evio.0",
45
    description=0x7fab188f004f "")
46
    at /home/ole/Develop/analyzer/Podd/THaRun.cxx:39
47
39	  fSegment(-1), fStream(-1)
48
(gdb) p fname
49
$1 = 0x7fab188f002c "/data/raw/run_7211.evio.0"
50
(gdb)
51
</pre>
52
Here, we printed (shorthand <code>p</code>) the contents of the variable <code>fname</code>. To end the session, type <code>q</code> in the debugger window.
53
54
The process is very similar under macOS with <code>lldb</code>, although the syntax of some <code>lldb</code> commands differes from that of <code>gdb</code>. You can find side-by-side comparisons of equivalent commands online. Obviously, this only scratches the surface of what is possible with a debugger. If you are unfamiliar with <code>gdb</code> or <code>lldb</code>, there are plenty of tutorials available online that should get you started quickly.