Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valgrind does not show line-numbers

I'm trying to find out where I made invalid writes to a piece of memory using Valgrind. It tells there is such an issue, also in what function, but not in what line. Although the function is quite small, I'd like to have the line-number shown in Valgrind. I've seen this on some outputs of Valgrind, but currently they are not shown, and I wonder why.

The output is the following:

niklas@emerald:~/Arbeitsfläche/spyr/bin/Debug$ valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./spyr
[...]
==4404== Invalid write of size 4
==4404==    at 0x8048849: sp_ParticleBuffer_init (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
==4404==    by 0x8048BFC: sp_ParticleSystem_createParticle (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
==4404==    by 0x8048691: main (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
==4404==  Address 0x422a0a0 is 4 bytes after a block of size 4 alloc'd
==4404==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==4404==    by 0x8048BC1: sp_ParticleSystem_createParticle (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
==4404==    by 0x8048691: main (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
==4404== 
==4404== Invalid write of size 4
==4404==    at 0x8048865: sp_ParticleBuffer_init (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
==4404==    by 0x8048BFC: sp_ParticleSystem_createParticle (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
==4404==    by 0x8048691: main (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
==4404==  Address 0x422a09c is 0 bytes after a block of size 4 alloc'd
==4404==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==4404==    by 0x8048BC1: sp_ParticleSystem_createParticle (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
==4404==    by 0x8048691: main (in /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr)
[...]

I saw outputs where the line number is shown after a double-colon behind the file-name. I.e. /home/niklas/Arbeitsfläche/spyr/bin/Debug/spyr:23 or similar.

How can I enable this?

FYI, this is the sp_ParticleBuffer_init function.

int sp_ParticleBuffer_init(sp_ParticleBuffer* buffer, sp_Uint32 buffer_size, int init_zero) {
    size_t size   = sizeof(sp_Particle) * buffer_size;
    buffer->next  = null;
    buffer->array = (sp_Particle*) malloc(size);
    buffer->alive_count = 0;

    if (!buffer->array) return SPYR_ALLOCFAILED;
    if (init_zero) memset((void*) buffer->array, 0, size);
    return SPYR_NOERR;
}
like image 940
Niklas R Avatar asked Aug 26 '12 12:08

Niklas R


People also ask

How do I show line numbers in Valgrind?

Look for function names and line numbersIf you compile your program with the -g flag, Valgrind will show you the function names and line numbers where errors occur.

Can Valgrind be wrong?

Yes, there are false positives with Valgrind, that's why it has suppression files for particular glibc and gcc versions, for example.

What are the errors in Valgrind?

Valgrind reports two types of issues: memory errors and memory leaks. When a program dynamically allocates memory and forgets to later free it, it creates a leak. A memory leak generally won't cause a program to misbehave, crash, or give wrong answers, and is not an urgent situation.

How do you run Valgrind with command line arguments?

Valgrind is installed on the department machines. To invoke it on an executable called a. out, you simply run the command valgrind ./a. out (with any arguments your program might need).


1 Answers

You need to include debug information in your binaries. Pass the -g flag if you're using gcc.

like image 97
cnicutar Avatar answered Sep 28 '22 07:09

cnicutar