La variabile d'ambiente LD_DEBUG (2012)
Sintesi redazionale: URL dell'articolo: https://bnikolic.co.uk/blog/linux-ld-debug.html URL dei commenti: https://news.ycombinator.com/item?id=48464330 Punti: 19 # Commenti: 0. Fonte originale: https://bnikolic.co.uk/blog/linux-ld-debug.html
<p># The LD_DEBUG environment variable</p><p>*Originally published 23 April 2012, updated 10 September 2019 (add<br>link to troubleshooting tool), updated 18 October 2019 (moved to<br>Jekyll), updated 3 April 2023 with links to other tools*</p><p>Development of large systems using many shared (dynamically) loaded libraries can sometimes lead to some frustrating bugs that are difficult to diagnose. These bugs often arise because there a few different versions of libraries on the system and the “wrong” version gets loaded instead of the one the developer used wanted.</p><p>You may also find of use my online tool for diagnosing linker problems on Linux implemented as an expert system. It is being expanded at the time of writing this but probably useful already.Note</p><p>I used to often debug these problems reasonably efficiently using the<br>`strace`</p><p>command (man strace)<br>and checking which libraries are getting accessed. However, there is a<br>much more efficient but perhaps not very well known way of debugging<br>shared library loading problems: the `LD_DEBUG`</p><p>environment<br>variable.</p><p>If the `LD_DEBUG`</p><p>variable is set then the Linux dynamic linker will<br>dump debug information which can be used to resolve most loading<br>problems very quickly. To see the available options just run any<br>program with the variable set to `help`</p><p>, i.e.:</p><p>```<br>LD_DEBUG=help cat<br>Valid options for the LD_DEBUG environment variable are:<br>libs display library search paths<br>reloc display relocation processing<br>files display progress for input file<br>symbols display symbol table processing<br>bindings display information about symbol binding<br>versions display version dependencies<br>all all previous options combined<br>statistics display relocation statistics<br>unused determined unused DSOs<br>help display this help message and exit<br>To direct the debugging output into a file instead of standard output<br>a filename can be specified using the LD_DEBUG_OUTPUT environment<br>variable.<br>```</p><p>There are many other useful tools to deal with linking issues:Note</p><p>- The<br>`strace`</p><p>program provides insight into all system calls, including searching, opening dynamic libraries- The<br>`ldd`</p><p>program resolves dynamic library dependencies- The<br>`objdump -x YOURFILE | grep NEEDED`</p><p>command lists the records in the program or library showing which other libraries are needed- The<br>`patchelf`</p><p>program makes it easy to change the`rpath`</p><p>of an ELF executable, changing the built-in search order- The<br>`LD_PRELOAD`</p><p>environment allows easy substitution of dynamic librariesNeed further advice? With over 20 years of experience we are uniquely placed to help – contact us at webs@bnikolic.co.uk</p><p>This variable is available Linux only. On MS Windows similar information can be obtained by enablingNote“Show Loader Snaps”(https://abitofscotland.wordpress.com/2019/12/20/dll-load-failure/) using the`gflags.exe`</p><p>program to enable this and then using`windbg`</p><p>to view the log. For example:</p><p>- Install Windows SDK for<br>`gflags`</p><p>and`windbg`</p><p>`winget install Microsoft.WindowsSDK.10.0.19041`</p><p>(or download from Microsoft website )- Enable ShowLoaderSnaps for an executable. Example: to enable for<br>`notepad.exe`</p><p>for example do in Powershell`&"C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\gflags.exe" /i notepad.exe +sls`</p><p>- Execute<br>`notepad.exe`</p><p>under WinDbg app. Detailed DLL loading log will be displayed</p><p>And here is an example run on my laptop:</p><p>```<br>LD_DEBUG=all cat<br>28504:<br>28504: file=libc.so.6 [0]; needed by cat [0]<br>28504: find library=libc.so.6 [0]; searching<br>28504: search path=/home/bnikolic/s/lib/tls/x86_64:/home/bnikolic/s/lib/tls:/home/bnikolic/s/lib/x86_64:/home/bnikolic/s/lib:tls/x86_64:tls:x86_64::/home/bnikolic/d/p/R-2.14.1bin/lib/R/lib/tls/x86_64:/home/bnikolic/d/p/R-2.14.1bin/lib/R/lib/tls:/home/bnikolic/d/p/R-2.14.1bin/lib/R/lib/x86_64:/home/bnikolic/d/p/R-2.14.1bin/lib/R/lib (LD_LIBRARY_PATH)<br>28504: trying file=/home/bnikolic/s/lib/tls/x86_64/libc.so.6<br>28504: trying file=/home/bnikolic/s/lib/tls/libc.so.6<br>28504: trying file=/home/bnikolic/s/lib/x86_64/libc.so.6<br>28504: trying file=/home/bnikolic/s/lib/libc.so.6<br>28504: trying file=tls/x86_64/libc.so.6<br>28504: trying file=tls/libc.so.6<br>28504: trying file=x86_64/libc.so.6<br>28504: trying file=libc.so.6<br>28504: trying file=/home/bnikolic/d/p/R-2.14.1bin/lib/R/lib/tls/x86_64/libc.so.6<br>28504: trying file=/home/bnikolic/d/p/R-2.14.1bin/lib/R/lib/tls/libc.so.6<br>28504: trying file=/home/bnikolic/d/p/R-2.14.1bin/lib/R/lib/x86_64/libc.so.6<br>28504: trying file=/home/bnikolic/d/p/R-2.14.1bin/lib/R/lib/libc.so.6<br>28504: search cache=/etc/ld.so.cache<br>28504: trying file=/lib/x86_64-linux-gnu/libc.so.6<br>28504:<br>28504: file=libc.so.6 [0]; generating link map<br>28504: dynamic: 0x00007fc38e525b40 base: 0x00007fc38e18c000 size: 0x00000000003a0368<br>28504: entry: 0x00007fc38e1ad420 phdr: 0x00007fc38e18c040 phnum: 10<br>28504:<br>28504: checking for version `GLIBC_2.4' in file /lib/x86_64-linux-gnu/libc.so.6 [0] required by file cat [0]<br>28504: checking for version `GLIBC_2.3' in file /lib/x86_64-linux-gnu/libc.so.6 [0] required by file cat [0]<br>28504: checking for version `GLIBC_2.3.4' in file /lib/x86_64-linux-gnu/libc.so.6 [0] required by file cat [0]<br>28504: checking for version `GLIBC_2.2.5' in file /lib/x86_64-linux-gnu/libc.so.6 [0] required by file cat [0]<br>28504: checking for version `GLIBC_2.3' in file /lib64/ld-linux-x86-64.so.2 [0] required by file /lib/x86_64-linux-gnu/libc.so.6 [0]<br>28504: checking for version `GLIBC_PRIVATE' in file /lib64/ld-linux-x86-64.so.2 [0] required by file /lib/x86_64-linux-gnu/libc.so.6 [0]<br>28504:<br>28504: relocation processing: /lib/x86_64-linux-gnu/libc.so.6 (lazy)<br>28504: symbol=_res; lookup in file=cat [0]<br>28504: symbol=_res; lookup in file=/lib/x86_64-linux-gnu/libc.so.6 [0]<br>28504: binding file /lib/x86_64-linux-gnu/libc.so.6 [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `_res' [GLIBC_2.2.5]<br>28504: symbol=_IO_file_close; lookup in file=cat [0]<br>28504: symbol=_IO_file_close; lookup in file=/lib/x86_64-linux-gnu/libc.so.6 [0]<br>28504: binding file /lib/x86_64-linux-gnu/libc.so.6 [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `_IO_file_close' [GLIBC_2.2.5]<br>28504: symbol=stderr; lookup in file=cat [0]<br>28504: binding file /lib/x86_64-linux-gnu/libc.so.6 [0] to cat [0]: normal symbol `stderr' [GLIBC_2.2.5]<br>28504: symbol=error_one_per_line; lookup in file=cat [0]<br>28504: symbol=error_one_per_line; lookup in file=/lib/x86_64-linux-gnu/libc.so.6 [0]<br>28504: binding file /lib/x86_64-linux-gnu/libc.so.6 [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `error_one_per_line' [GLIBC_2.2.5]<br>28504: symbol=__malloc_initialize_hook; lookup in file=cat [0]<br>28504: symbol=__malloc_initialize_hook; lookup in file=/lib/x86_64-linux-gnu/libc.so.6 [0]<br>28504: binding file /lib/x86_64-linux-gnu/libc.so.6 [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `__malloc_initialize_hook' [GLIBC_2.2.5]<br>28504: symbol=__morecore; lookup in file=cat [0]<br>28504: symbol=__morecore; lookup in file=/lib/x86_64-linux-gnu/libc.so.6 [0]<br>28504: binding file /lib/x86_64-linux-gnu/libc.so.6 [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `__morecore' [GLIBC_2.2.5]<br>28504: symbol=__key_encryptsession_pk_LOCAL; lookup in file=cat [0]<br>28504: symbol=__key_encryptsession_pk_LOCAL; lookup in file=/lib/x86_64-linux-gnu/libc.so.6 [0]<br>28504: binding file /lib/x86_64-linux-gnu/libc.so.6 [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `__key_encryptsession_pk_LOCAL' [GLIBC_2.2.5]<br>28504: symbol=__progname_full; lookup in file=cat [0]<br>28504: binding file /lib/x86_64-linux-gnu/libc.so.6 [0] to cat [0]: normal symbol `__progname_full' [GLIBC_2.2.5]<br>28504: symbol=__ctype32_tolower; lookup in file=cat [0]<br>28504: symbol=__ctype32_tolower; lookup in file=/lib/x86_64-linux-gnu/libc.so.6 [0]<br>28504: binding file /lib/x86_64-linux-gnu/libc.so.6 [0] to /lib/x86_64-linux-gnu/libc.so.6 [0]: normal symbol `__ctype32_tolowe</p><p>_(testo troncato)_</p>
