C/C++ mprof (Memory Profiler)

AlexanderProphet

Active Member

Reaction score: 35
Messages: 156

Hullo there FreeBSD community,
I have written an application based on mprof, the memory profiler for Unix which was last updated in about 1989.
It's released under a BSD style license. It compiles unmodified on both Linux and FreeBSD.
Currently it only supports AMD64 processors.
I'd be extremely grateful if people could test it out on their C programs and report any bugs/crashes etc.. To fetch and install:
git clone https://www.github.com/SanctaMaria1997/mprof.git mprof
cd mprof
make
sudo make install

I'll just leave you with some excerpts from the README and the manual page.

mprof

C Memory Profiler

mprof is a rewrite of a classic Unix tool which analyses a C program's memory usage and prints out several detailed reports.

  • Find memory leaks
  • Quickly see which C files were responsible for leaks (including exact line numbers)
  • Find out which functions (or chains of functions) are allocating/freeing/leaking the most memory (in terms of both function calls and bytes)
  • Get a breakdown of memory chunk sizes to understand how your program uses memory
  • Find out which C structs in your source code correspond to the memory your program allocates.

DESCRIPTION
mprof is a utility for examining the memory usage of C programs. It works by patching the libc functions
malloc(), calloc(), realloc() and free(); these functions are replaced with equivalents that gather statis‐
tics on memory (mis)use. When the program has finished executing, mprof outputs a memory usage report to a
file (this can be either a physical disk file or the error stream stderr).
 

Emrion

Active Member

Reaction score: 40
Messages: 194

Tried:
$ mprof
[mprof] Unable to lauch program .
Segmentation fault (core dumped)


Ok, I know that I'd to put a program in the command line and If I do that, there is no error.
But please, correct your english and avoid a segmentation fault. As this is the kind of detail which distrust people from your program.
 
OP
OP
AlexanderProphet

AlexanderProphet

Active Member

Reaction score: 35
Messages: 156

Thanks. I will fix the typo at the next commit. I was aware of the segfault, but as you say it only happens with wrong input. I will fix it though.
Did everything else work as expected?
 

Emrion

Active Member

Reaction score: 40
Messages: 194

I don't think it works. I tried it on an old program I wrote in C++, a simple http proxy. My software runs well but mprof seems to gather nothing. Maybe I missed something, a prerequisite?

[mprof] MEMORY STATISTICS
[mprof] Memory usage summary:
[mprof] Program allocated 0 block(s)
[mprof] (malloc: 0, calloc: 0, realloc: 0)
[mprof] Program freed 0 block(s)
[mprof] For detailed memory usage statistics, consult the file "tables.mprof.5".
[mprof] Program exited with code 0.


Content of tables.mprof.5:

[mprof] TABLE 1: ALLOCATION BINS

size allocs bytes (%) frees kept (%) types

[mprof] TABLE 2: MEMORY LEAKS

kept bytes (%) allocs bytes (%) frees bytes (%) path

[mprof] TABLE 3: DIRECT_ALLOCATION

% mem bytes % mem(size) bytes kept % all kept c
s m l x s m l x
 
OP
OP
AlexanderProphet

AlexanderProphet

Active Member

Reaction score: 35
Messages: 156

Hi there,
It is only intended to work with C programs. It is malfunctioning however, as it should fail and print out a message saying "this is not a C program". It looks for the language in the DWARF debug info.
I'm thinking perhaps your app wasn't compiled with the -g option and/or wasn't compiled with clang?
In any case it wouldn't print anything useful as it doesn't patch the new/delete operators - only malloc/calloc/realloc/free.
Thanks for your help.
 

Emrion

Active Member

Reaction score: 40
Messages: 194

I think that C++ programs are C ones in the root. new operator calls malloc.

Anyway, I tried to compile it with the -g option but I got the same result.

Here is my (modified) Makefile:
CXXFLAGS = -Wall -Wextra -pedantic -ansi
objets = relais.o relais_coeur.o kparser.o

install: $(objets)
cc -g -o relais $(objets) -pthread -lstdc++

clean:
rm $(objets)

depend:
cc -E -MM *.cpp > .depend
 
OP
OP
AlexanderProphet

AlexanderProphet

Active Member

Reaction score: 35
Messages: 156

Hi Emrion,
It is strange that mprof behaves the way it does with a C++ app. I don't really want to debug it though because that's not its intended use. The DWARF parser doesn't even look for classes - only structures. There would be other issues too probably - I'd need to consult the DWARF specifications. I'm sure that (as you said) new and delete use malloc and free internally - however this would render mprof's output useless because all leaks would appear to happen within the implementation of "new".
I am very grateful for your time and feedback though. And if you or anyone else could test mprof with real-world C programs I'd like that.
 
OP
OP
AlexanderProphet

AlexanderProphet

Active Member

Reaction score: 35
Messages: 156

I've recently fixed about 10 serious bugs and optimisation issues so if mprof wasn't working for you before it is definitely worth another git clone
It now supports debugging of GNU glib's memory functions as well as the standard C library. I've successfully debugged the popular IRC client "hexchat" on Fedora, so stability is definitely improving - hexchat is a complex multi-threaded GUI program that loads about 20 .so files.
mprof also now patches libraries automatically if their .so files are in the present working directory (or below).
Here are some screenshots:
6544

6545
 
Top