CarloWood / libmemleak

A shared library that can be LD_PRELOAD-ed to show memory increments per second per backtrace on dynamically chosen time intervals.
GNU General Public License v3.0
26 stars 10 forks source link

Binutils version 2.34, pthread error & "help" message questions. #8

Open Prikalel opened 3 years ago

Prikalel commented 3 years ago

Sorry for my bad English, but I have a couple of questions: 1) When building with binutils 2.34:

$ ld -v
GNU ld (GNU Binutils for Ubuntu) 2.34

I had warnings in this lines:

addr2line.c: In function ‘find_address_in_section’:
addr2line.c:287:8: warning: implicit declaration of function ‘bfd_get_section_flags’; did you mean ‘bfd_set_section_flags’? [-Wimplicit-function-declaration]
  287 |   if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
      |        ^~~~~~~~~~~~~~~~~~~~~
      |        bfd_set_section_flags
addr2line.c:290:9: warning: implicit declaration of function ‘bfd_get_section_vma’; did you mean ‘bfd_set_section_vma’? [-Wimplicit-function-declaration]
  290 |   vma = bfd_get_section_vma(abfd, section);
      |         ^~~~~~~~~~~~~~~~~~~
      |         bfd_set_section_vma
addr2line.c:294:10: warning: implicit declaration of function ‘bfd_get_section_size’; did you mean ‘bfd_set_section_size’? [-Wimplicit-function-declaration]
  294 |   size = bfd_get_section_size(section);
      |          ^~~~~~~~~~~~~~~~~~~~
      |          bfd_set_section_size

By serfing a net for some time I managed to get around by replacing some code in addr2line.c:

bfd_get_section_flags(abfd, section) => bfd_section_flags(section)
bfd_get_section_vma(abfd, section) => bfd_section_vma(section)
bfd_get_section_size(section) => bfd_section_size(section)

After that it has built with no warnings.

My "advice" for the project' manager: In other projects they add a macro check in configure.ac file and create a #define macro in config.h in case the new biutils API is installed on the user's machine. After it just add next lines to your code:

#if HAVE_OLD_BFD_VERSION
#  define my_bfd_section_vma(_abfd, _section) \
    bfd_get_section_vma(_abfd, _section)
#else
#  define my_bfd_section_vma(_abfd, _section) \
    bfd_section_vma(_section)
#endif

and use my_bfd_section_vma in your code :)

2) When you compile the program without -pthread option, you'll probably get this message:

$ LD_PRELOAD='/usr/local/lib/libmemleak.so /usr/lib/x86_64-linux-gnu/libdl.so /usr/lib/x86_64-linux-gnu/l
ibbfd.so' ./my_prog
Entering addr2line_init
(Range){begin:0x5565572a0000, end:0x5565572a2000} : /home/alex/Документы/linuxapplicationdevelopment2021/07_Environmental/my_prog
(Range){begin:0x7f83a0e74000, end:0x7f83a0e86000} : /lib/x86_64-linux-gnu/libgcc_s.so.1
(Range){begin:0x7f83a0e9d000, end:0x7f83a0eac000} : /lib/x86_64-linux-gnu/libtinfo.so.6.2
(Range){begin:0x7f83a0ec1000, end:0x7f83a0ed2000} : /lib/x86_64-linux-gnu/libz.so.1.2.11
(Range){begin:0x7f83a0f02000, end:0x7f83a107a000} : /lib/x86_64-linux-gnu/libc-2.31.so
(Range){begin:0x7f83a10e3000, end:0x7f83a110c000} : /lib/x86_64-linux-gnu/libreadline.so.8.0
(Range){begin:0x7f83a1122000, end:0x7f83a1135000} : /usr/lib/x86_64-linux-gnu/librhash.so.0
(Range){begin:0x7f83a11ba000, end:0x7f83a126b000} : /usr/lib/x86_64-linux-gnu/libbfd-2.34-system.so
(Range){begin:0x7f83a12c0000, end:0x7f83a12c2000} : /lib/x86_64-linux-gnu/libdl-2.31.so
(Range){begin:0x7f83a12c8000, end:0x7f83a12ce000} : /usr/local/lib/libmemleak.so.0.0.3
(Range){begin:0x7f83a1ad6000, end:0x7f83a1af9000} : /lib/x86_64-linux-gnu/ld-2.31.so
./my_prog: symbol lookup error: /usr/local/lib/libmemleak.so: undefined symbol: pthread_create

My way to compile my_prog was:

$ gcc  -O2 -Wall -Werror -Wno-pointer-sign -std=gnu11   -o my_prog src/obj1.o src/obj2.o src/obj3.o ...  -lrhash -lreadline

After building with pthread error gone and I managed to start it with LD_PRELOAD:

$ gcc  -O2 -Wall -Werror -Wno-pointer-sign -std=gnu11   -o my_prog src/obj1.o src/obj2.o src/obj3.o ...  -lrhash -lreadline -pthread
$ LD_PRELOAD='...' ./my_prog
Entering addr2line_init
...

3) Cool, but I have a REPL-apploication which I want to pass through the libmemleak to check it for leaks of memory. So after start recording in other terminal:

$ memleak_control
libmemleak> start

Libmemleak just started printing info every second and I wasn't able to print any command to my_prog. In help message there is a string:

stats    : Print overview of backtrace with highest leak probability.
stats N  : Automatically print stats every N seconds (use 0 to turn off).

I tryed stats 0 but get next message:

libmemleak> stats 0
Interval between printing of stats must be at least 1 second.

So how can I start recording but do not print any stats automatically?

CarloWood commented 1 year ago

Sorry for the extreme delay, but github never mailed me a notification and I just missed this issue until now.

This is currently not possible. If you need to do I/O using stdcout/stdin while doing leak detection then your best option is to add a command to libmemleak and memleak_control to suppress the output (but still do recording). The comment that using 'stats 0' would turn it off is just wrong :/.