### Description I want debug memory usage by TLS module. To do this used command ``` make -C src/modules/tls CFLAGS="-DTLS_MALLOC_DBG=1" ``` And this output ``` [root@sbc-stage-a0 kamailio]# make -C src/modules/tls CFLAGS="-DTLS_MALLOC_DBG=1" make: Entering directory '/root/bak/kamailio/src/modules/tls' CC (gcc) [M tls.so] tls_domain.o CC (gcc) [M tls.so] tls_rand.o CC (gcc) [M tls.so] tls_map.o CC (gcc) [M tls.so] tls_cfg.o CC (gcc) [M tls.so] tls_mod.o CC (gcc) [M tls.so] tls_util.o CC (gcc) [M tls.so] tls_config.o CC (gcc) [M tls.so] tls_server.o CC (gcc) [M tls.so] tls_select.o CC (gcc) [M tls.so] tls_dump_vf.o CC (gcc) [M tls.so] tls_init.o tls_init.c:137:2: warning: #warning "tls module compiled with malloc debugging info (extra overhead)" [-Wcpp] #warning "tls module compiled with malloc debugging info (extra overhead)" ^~~~~~~ tls_init.c: In function ‘ser_malloc’: tls_init.c:218:5: warning: implicit declaration of function ‘_shm_malloc’; did you mean ‘shm_malloc’? [-Wimplicit-function-declaration] p=_shm_malloc(size+s, file, "via ser_malloc", line); ^~~~~~~~~~~ shm_malloc tls_init.c:218:4: warning: assignment to ‘void *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion] p=_shm_malloc(size+s, file, "via ser_malloc", line); ^ tls_init.c:224:39: error: invalid application of ‘sizeof’ to incomplete type ‘struct qm_frag’ ((struct qm_frag*)((char*)p-sizeof(struct qm_frag)))->func= ^~~~~~ tls_init.c: In function ‘ser_realloc’: tls_init.c:253:5: warning: implicit declaration of function ‘_shm_realloc’; did you mean ‘shm_realloc’? [-Wimplicit-function-declaration] p=_shm_realloc(ptr, size+s, file, "via ser_realloc", line); ^~~~~~~~~~~~ shm_realloc tls_init.c:253:4: warning: assignment to ‘void *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion] p=_shm_realloc(ptr, size+s, file, "via ser_realloc", line); ^ tls_init.c:259:39: error: invalid application of ‘sizeof’ to incomplete type ‘struct qm_frag’ ((struct qm_frag*)((char*)p-sizeof(struct qm_frag)))->func= ^~~~~~ tls_init.c: In function ‘tls_pre_init’: tls_init.c:684:7: warning: implicit declaration of function ‘CRYPTO_set_mem_ex_functions’; did you mean ‘CRYPTO_set_mem_functions’? [-Wimplicit-function-declaration] if (!CRYPTO_set_mem_ex_functions(ser_malloc, ser_realloc, ser_free)) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~ CRYPTO_set_mem_functions tls_init.c:684:60: error: ‘ser_free’ undeclared (first use in this function); did you mean ‘sk_free’? if (!CRYPTO_set_mem_ex_functions(ser_malloc, ser_realloc, ser_free)) { ^~~~~~~~ sk_free tls_init.c:684:60: note: each undeclared identifier is reported only once for each function it appears in make: *** [../../Makefile.rules:100: tls_init.o] Error 1 make: Leaving directory '/root/bak/kamailio/src/modules/tls' ```
### Additional Information kamailio 5.6.4 with some customisation
Thanks for the report. I can also reproduce it on git master. I tested some older Kamailio versions, back to version 4.4.x. It was broken already then. It probably makes more sense to just remove it, as nobody seems to use it for at least 5 years. Lets wait a bit for other opinion, if people want to keep it.
@ivanuschak could you post your hotfix for reiew?
@sergey-safarov the build fix can be found in this commit - https://github.com/ivanuschak/kamailio/commit/aad84394db96756e5cdc423d64bc38...
But that is just a temporary fix to make build with TLS_MALLOC_DBG flag successful for centos 8, but that is not something can be posted for review and further merge, becasue the implementation needs to be more correct.
I have started Lamailio module with patch provied above. To start Kamailio used command with '-ddd -E' and output send to tmp logfile. Then send curl https request to Kamailio tls port. And then greped logfile using command ``` grep alloc /tmp/logfile | grep -v db_row| grep -v db_val | grep -v db_res ``` I got this result
``` 0(4032186) DEBUG: <core> [core/mem/q_malloc.c:199]: qm_malloc_init(): QM_OPTIMIZE=16384, /ROUNDTO=2048 0(4032186) DEBUG: <core> [core/mem/q_malloc.c:201]: qm_malloc_init(): QM_HASH_SIZE=2099, qm_block size=235152 0(4032186) DEBUG: <core> [core/mem/q_malloc.c:203]: qm_malloc_init(): qm_malloc_init(0xffffb36f0010, 25165824), start=0xffffb36f0010 0(4032186) DEBUG: <core> [core/mem/q_malloc.c:212]: qm_malloc_init(): size= 25165824, init_overhead=235256 0(4032186) DEBUG: <core> [core/mem/q_malloc.c:199]: qm_malloc_init(): QM_OPTIMIZE=16384, /ROUNDTO=2048 0(4032186) DEBUG: <core> [core/mem/q_malloc.c:201]: qm_malloc_init(): QM_HASH_SIZE=2099, qm_block size=235152 0(4032186) DEBUG: <core> [core/mem/q_malloc.c:203]: qm_malloc_init(): qm_malloc_init(0xffffab400000, 134217728), start=0xffffab400000 0(4032186) DEBUG: <core> [core/mem/q_malloc.c:212]: qm_malloc_init(): size= 134217728, init_overhead=235256 0(4032186) DEBUG: <core> [db_query.c:412]: db_query_init(): About to allocate sql_buf size = 65535 0(4032186) DEBUG: <core> [core/mem/pkg.c:109]: pkg_print_manager(): pkg - using memory manager: q_malloc 0(4032186) DEBUG: <core> [core/mem/shm.c:301]: shm_print_manager(): shm - using memory manager: q_malloc 0(4032186) DEBUG: <core> [core/mem/shm.c:289]: shm_destroy_manager(): destroying memory manager: q_malloc 0(4032186) DEBUG: <core> [core/mem/q_malloc.c:1260]: qm_shm_lock_destroy(): destroying the shared memory lock 0(4032186) DEBUG: <core> [core/mem/pkg.c:98]: pkg_destroy_manager(): destroying memory manager: q_malloc ``` Not much info about memory allocation.
Found need to execute kamailio with `-dddd` flag. Now I can see messages like ``` 0(4041924) DEBUG: <core> [core/mem/q_malloc.c:526]: qm_free(): freeing frag. 0xffff9f2fa8e0 alloc'ed from crypto/lhash/lhash.c: +0x138a34:ERR_load_strings_const+0x20:ERR_load_ENGINE_strings+0x38:+0x13a150:+0x15b474:+0xf7a4:CRYPTO_THREAD_run_once+0xc:OPENSSL_init_crypto+0x1a8:ERR_get_state+0x104:ERR_clear_error+0xc:+0x15b34c:+0xf7a4:CRYPTO_THREAD_run_once+0xc:OPENSSL_init_crypto+0x10c:+0xcfde4:+0x15b3f4:+0xf7a4:CRYPTO_THREAD_run_once+0xc:OPENSSL_init_crypto+0x298:OPENSSL_init_ssl+0x3c:tls_h_mod_pre_init_f+0xb0c:pre_init_tls+0x2c:main+0x98bc:__libc_start_main+0xdc:(225) ``` But allocation strings for the same fragment look like ``` 0(4041924) DEBUG: <core> [core/mem/q_malloc.c:419]: qm_malloc(): qm_malloc(0xffff9f120000, 16832) returns address 0xffff9f2fa918 frag. 0xffff9f2fa8e0 (size=16832) on 1 -th hit ```
Looks like tracing print only for `free` operaions and not preint for `alloc` operations.
I pushed some commits trying to revive this compile option. It still requires q_malloc (qm) to be used, as it had some code specific to that memory manager (now made more generic, but not implemented for the other memory managers).
Try with master branch and see if works.
Closed #3532 as completed.