Debugging » History » Version 17
Jaroslav Kysela, 2017-05-05 12:17
1 | 1 | Adam Sutton | h1. Debugging |
---|---|---|---|
2 | |||
3 | If you're going to be regularly trying development versions of Tvheadend or need to report a crash or deadlock then you should really read this page! |
||
4 | |||
5 | 7 | Jaroslav Kysela | If you are investigating problems within Tvheadend then its worth being familiar with tools such as gdb and valgrind or clang, although these are not covered here. |
6 | 1 | Adam Sutton | |
7 | However one thing that can be useful in investigating crashes within Tvheadend is to ensure that coredumps are generated, this will allow post analysis in gdb without having to actual run Tvheadend within gdb. |
||
8 | |||
9 | You can enable temporarily by running: |
||
10 | |||
11 | <pre> |
||
12 | ulimit -c unlimited |
||
13 | </pre> |
||
14 | |||
15 | To make this permanent put this somewhere in your shell environment setup (.bashrc, .profile, etc...) |
||
16 | Firstly I'd recommend that if you're specifically trying to investigate an issue then you should consider running Tvheadend manually, rather than as a service, as documented [[Development|here]]. |
||
17 | |||
18 | h2. Logging |
||
19 | |||
20 | I'd strongly recommend that if you're specifically trying to investigate a crash or other problem in Tvheadend that you enable debugging: |
||
21 | |||
22 | * *-s* will output debug info to syslog |
||
23 | * *--debug* allows you to specify which subsystem to debug (TODO: add more info) |
||
24 | * *--trace* allows you to enable trace (more in-depth) logging on specific subsystems |
||
25 | |||
26 | You can also get Tvheadend to log to it's own file using: |
||
27 | |||
28 | <pre> |
||
29 | -l FILE |
||
30 | </pre> |
||
31 | |||
32 | 2 | Jaroslav Kysela | You may also modify the debug settings using WEB GUI as admin - Configuration/Debugging. Note that the information is not saved, |
33 | it is just set for run-time (current task). |
||
34 | |||
35 | * Debug log path - filename to store log |
||
36 | * Debug trace - enable traces |
||
37 | * Debug subsystems - comma separated list of subsystems |
||
38 | * Trace subsystems - comma separated list of subsystems |
||
39 | |||
40 | 3 | Jaroslav Kysela | The traces must be compiled to the tvheadend binary - see [[Traces]]. |
41 | 2 | Jaroslav Kysela | |
42 | 4 | Jaroslav Kysela | h2. Basic crash debug |
43 | |||
44 | You may run tvh in gdb directly using command: |
||
45 | |||
46 | <pre> |
||
47 | gdb --args /the standard tvh command line/ |
||
48 | 5 | Jaroslav Kysela | |
49 | (gdb) run |
||
50 | 4 | Jaroslav Kysela | </pre> |
51 | |||
52 | Or attach gdb to the running process: |
||
53 | |||
54 | <pre> |
||
55 | gdb tvheadend pid |
||
56 | 5 | Jaroslav Kysela | |
57 | (gdb) continue |
||
58 | 4 | Jaroslav Kysela | </pre> |
59 | |||
60 | 17 | Jaroslav Kysela | The 'continue' command will continue the execution of the program. If you need to _break_ the execution and return to gdb, just use 'Ctrl-C'. |
61 | |||
62 | 4 | Jaroslav Kysela | You may need to replace _tvheadend_ with the full path to the binary and you will need to replace _pid_ with the PID of the running process. To find that run: |
63 | |||
64 | <pre> |
||
65 | ps -C tvheadend |
||
66 | </pre> |
||
67 | |||
68 | Once you have gdb attached grab a stack trace from every thread using the following command: |
||
69 | |||
70 | <pre> |
||
71 | (gdb) set logging on |
||
72 | (gdb) set pagination off |
||
73 | (gdb) bt full |
||
74 | </pre> |
||
75 | |||
76 | Note: "set logging on" will cause GDB to write its output to a file, by default this will be gdb.txt in the current directory. |
||
77 | |||
78 | 1 | Adam Sutton | h2. Enabling coredumps |
79 | |||
80 | 4 | Jaroslav Kysela | If you need to investigate some running problem you can always attach (see below) later and if you need to trap crashes, then you can configure your system to generate a core file and then retrospectively analyse this with gdb. |
81 | 1 | Adam Sutton | |
82 | If you're running manually you should enable coredumps in your environment: |
||
83 | |||
84 | <pre> |
||
85 | ulimit -c unlimited |
||
86 | </pre> |
||
87 | |||
88 | I'd recommend you enable this permanently by putting this command in your shell initialisation scripts (.bashrc etc..). |
||
89 | |||
90 | If you're running as a daemon then you should use the -D command line option, this will enable coredumps from the daemon. If you start using sysvinit, upstart etc... then you will need to put this in the configuration file, e.g.: |
||
91 | |||
92 | <pre> |
||
93 | TVH_ARGS="-D" |
||
94 | </pre> |
||
95 | |||
96 | Finally it's probably worth changing the coredump file format, personally I use the following configuration: |
||
97 | |||
98 | <pre> |
||
99 | echo core.%h.%e.%t | sudo tee /proc/sys/kernel/core_pattern |
||
100 | echo 0 | sudo tee /proc/sys/kernel/core_uses_pid |
||
101 | </pre> |
||
102 | |||
103 | Or put the following in /etc/sysctl.conf: |
||
104 | |||
105 | <pre> |
||
106 | kernel.core_pattern = core.%h.%e.%t |
||
107 | kernel.core_uses_pid = 0 |
||
108 | </pre> |
||
109 | |||
110 | If you're using a system like Ubuntu that uses apport (and cripples the ability to change the core format) just set core_uses_pid=1 instead. |
||
111 | |||
112 | Note: coredumps are (by default) stored in the current working directory, to make it possible for the daemon to write files the current working directory is set to /tmp when using -D, so check there for core files. |
||
113 | |||
114 | To verify that you have everything configured properly you can use the -A option to force a crash on startup. Do this from the command line or add to /etc/default/tvheadend: |
||
115 | |||
116 | <pre> |
||
117 | TVH_ARGS="-D -A" |
||
118 | </pre> |
||
119 | |||
120 | Note: remember to remove the option after you've tested it! |
||
121 | |||
122 | h2. Processing core file. |
||
123 | |||
124 | Once you have a core file you can start up gdb with that coredump, just as if you'd caught the crash while running under gdb: |
||
125 | |||
126 | <pre> |
||
127 | gdb tvheadend core |
||
128 | </pre> |
||
129 | |||
130 | You may need to replace _tvheadend_ and _core_ above with the proper paths. |
||
131 | |||
132 | For most crashes the most useful information is the back trace, this will provide a stack trace showing where the code crashed and the stack information at the time of the crash: |
||
133 | |||
134 | <pre> |
||
135 | (gdb) set logging on |
||
136 | (gdb) set pagination off |
||
137 | (gdb) bt full |
||
138 | #0 0x00007f5b10cc1425 in __GI_raise (sig=<optimised out>) |
||
139 | at ../nptl/sysdeps/unix/sysv/linux/raise.c:64 |
||
140 | resultvar = 0 |
||
141 | pid = <optimised out> |
||
142 | selftid = 7517 |
||
143 | #1 0x00007f5b10cc4b10 in __GI_abort () at abort.c:120 |
||
144 | act = {__sigaction_handler = {sa_handler = 0, sa_sigaction = 0}, |
||
145 | sa_mask = {__val = {18446744073709551615 <repeats 16 times>}}, |
||
146 | sa_flags = 0, sa_restorer = 0} |
||
147 | sigs = {__val = {32, 0 <repeats 15 times>}} |
||
148 | #2 0x000000000040744e in main (argc=<optimised out>, argv=<optimised out>) |
||
149 | at src/main.c:810 |
||
150 | i = <optimised out> |
||
151 | set = {__val = {16386, 0 <repeats 15 times>}} |
||
152 | adapter_mask = <optimised out> |
||
153 | log_level = <optimised out> |
||
154 | log_options = <optimised out> |
||
155 | log_debug = <optimised out> |
||
156 | log_trace = <optimised out> |
||
157 | buf = "/tmp\000\000\000\000\360\350\364\023[\177\000\000\000\320\365\023[\177\000\000t\n\327\023[\177\000\000\370\271\311\020[\177\000\000\017\000\000\000\000\000\000\000:\000\000\000\000\000\000\000h\344\364\023[\177\000\000.N=\366\000\000\000\000\236\022\327\023[\177\000\000\300\304S\205\377\177\000\000.\000\000\000\000\000\000\000 \305S\205\377\177\000\000\377\377\377\377\000\000\000\000\264\352\310\020[\177\000\000\250\354\310\020[\177\000\000\360\304S\205\377\177\000\000\360\350\364\023[\177\000\000@\256\311\020[\177", '\000' <repeats 18 times>"\340, \346\364\023[\177\000\000\000\320\365\023[\177\000\000\231,@\000\000\000\000\000\370\271\311\020[\177\000\000\340\033@\000\000\000\000\000\000\000\000\000\001\000\000\000\021\b\000\000\001", '\000' <repeats 11 times>, " \266\370\023[\177\000\000`\305S\205\377\177\000\000.N=\366\000\000\000\000\340\346\364\023[\177\000\000\200\305S\205\377\177\000\000"... |
||
158 | opt_help = 0 |
||
159 | opt_version = 0 |
||
160 | opt_fork = 1 |
||
161 | opt_firstrun = 0 |
||
162 | opt_stderr = 0 |
||
163 | opt_syslog = 0 |
||
164 | opt_uidebug = 0 |
||
165 | </pre> |
||
166 | |||
167 | Note: "set logging on" will cause GDB to write its output to a file, by default this will be gdb.txt in the current directory. |
||
168 | |||
169 | However I'd strongly recommend that you keep a copy of tvheadend binary and core file in case further analysis is required. |
||
170 | |||
171 | h2. Dead or Live Lock |
||
172 | |||
173 | If Tvheadend appears to die but the process is still running, then its quite possible that the process is deadlocked (or possibly live locked). The best way to help investigate such a problem is to get a full stack trace from every thread in the system. |
||
174 | |||
175 | First attach gdb to the running process: |
||
176 | |||
177 | <pre> |
||
178 | gdb tvheadend pid |
||
179 | 5 | Jaroslav Kysela | |
180 | (gdb) continue |
||
181 | 1 | Adam Sutton | </pre> |
182 | 17 | Jaroslav Kysela | |
183 | The 'continue' command will continue the execution of the program. If you need to _break_ the execution, just use 'Ctrl-C'. |
||
184 | 1 | Adam Sutton | |
185 | You may need to replace _tvheadend_ with the full path to the binary and you will need to replace _pid_ with the PID of the running process. To find that run: |
||
186 | |||
187 | <pre> |
||
188 | ps -C tvheadend |
||
189 | </pre> |
||
190 | |||
191 | Once you have gdb attached grab a stack trace from every thread using the following command: |
||
192 | |||
193 | <pre> |
||
194 | (gdb) set logging on |
||
195 | (gdb) set pagination off |
||
196 | (gdb) thread apply all bt full |
||
197 | </pre> |
||
198 | |||
199 | Note: "set logging on" will cause GDB to write its output to a file, by default this will be gdb.txt in the current directory. |
||
200 | |||
201 | It might also be useful to generate a core file for good measure: |
||
202 | |||
203 | <pre> |
||
204 | (gdb) generate-core-file |
||
205 | </pre> |
||
206 | |||
207 | This information may give an indication as to why things are locked, often 2 threads are stuck trying to lock a mutex (probably each holds the opposite lock). |
||
208 | |||
209 | h2. Reporting crash (or lock) |
||
210 | |||
211 | 6 | Jaroslav Kysela | If you're going to report a crash (or lockup) then please try to provide the above information, including a debug log (or whatever logging you have), a core file and the tvheadend binary and basic information about the platform (distribution, version and architecture) you're running on. |
212 | 8 | Jaroslav Kysela | |
213 | h1. Memory leaks or corruption |
||
214 | |||
215 | It may be really difficult to track these problems. There are basically two tools which may help to discover the memory leaks or memory corruptions. |
||
216 | |||
217 | h2. Valgrind |
||
218 | |||
219 | It is very slow, but it may be useable for things which are triggered everytime: |
||
220 | |||
221 | <pre> |
||
222 | valgrind --leak-check=full --show-reachable=yes /tvh_command_line/ |
||
223 | </pre> |
||
224 | |||
225 | h2. clang |
||
226 | |||
227 | 9 | Jaroslav Kysela | There is address sanitizer in the clang toolkit. The binary must be rebuild using the clang compiler and libraries: |
228 | 8 | Jaroslav Kysela | |
229 | <pre> |
||
230 | ARGS="/your_configure_arguments/" |
||
231 | SANITIZER=address |
||
232 | export CFLAGS="-fsanitize=$SANITIZER" |
||
233 | export LDFLAGS="-fsanitize=$SANITIZER" |
||
234 | ./configure $ARGS --disable-pie --enable-ccdebug python=python3 cc=clang ld=clang nowerror |
||
235 | make -j4 |
||
236 | </pre> |
||
237 | 10 | C K | |
238 | Example build script (build_with_clang.sh): |
||
239 | |||
240 | <pre> |
||
241 | #!/bin/sh |
||
242 | 16 | C K | make distclean |
243 | 10 | C K | ARGS="--enable-libffmpeg_static --disable-hdhomerun_static" |
244 | SANITIZER=address |
||
245 | export CFLAGS="-fsanitize=$SANITIZER" |
||
246 | export LDFLAGS="-fsanitize=$SANITIZER" |
||
247 | 11 | C K | ./configure $ARGS --disable-pie --enable-ccdebug python=python3 cc=clang ld=clang nowerror |
248 | 10 | C K | make -j4 |
249 | </pre> |
||
250 | |||
251 | Make sure to make your script executable. |
||
252 | 12 | Jaroslav Kysela | |
253 | If you do not see resolved the function names like: |
||
254 | |||
255 | <pre> |
||
256 | ==16673==WARNING: Trying to symbolize code, but external symbolizer is not initialized! |
||
257 | #0 0x7fcda9407680 (/home/tvh/src/tvheadend/build.linux/tvheadend+0x65b680) |
||
258 | #1 0x7fcda943b115 (/home/tvh/src/tvheadend/build.linux/tvheadend+0x68f115) |
||
259 | </pre> |
||
260 | |||
261 | 16 | C K | get the correct path for the llvm-symbolizer, i.e. with |
262 | <pre>whereis llvm-symbolizer</pre> |
||
263 | |||
264 | 12 | Jaroslav Kysela | then make sure that you set the external symbolizer like: |
265 | |||
266 | 1 | Adam Sutton | <pre> |
267 | 16 | C K | ASAN_OPTIONS=symbolize=1 ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer /home/ts/workspace/tvheadend/build.linux/tvheadend -l thv.log |
268 | 13 | Jaroslav Kysela | </pre> |
269 | |||
270 | The error log should be like: |
||
271 | |||
272 | <pre> |
||
273 | 14 | Jaroslav Kysela | ==27911==ERROR: AddressSanitizer: heap-use-after-free on address 0x60700000d928 at pc 0x56409f916af4 bp 0x7ffc463d6670 sp 0x7ffc463d6668 |
274 | READ of size 8 at 0x60700000d928 thread T0 |
||
275 | 15 | Jaroslav Kysela | #0 0x56409f916af3 in idnode_unlink /home/tvh/git/tvheadend/src/idnode.c:164:94 |
276 | #1 0x56409f9c9f8a in memoryinfo_unregister /home/tvh/git/tvheadend/src/memoryinfo.h:52:3 |
||
277 | #2 0x56409f9c9de2 in streaming_done /home/tvh/git/tvheadend/src/streaming.c:597:3 |
||
278 | 12 | Jaroslav Kysela | </pre> |