Escolar Documentos
Profissional Documentos
Cultura Documentos
Prepared by
Shreeji Sheth
gcc
Profiling: gprof,valgrind
gdb
Doxygen
Mallochelper
Eclipse
Cunit
gcc
-E to generate .i file which is about expansion of
library and preprocessor directives;also removal
of comments
-S to generate assembly code or .s file
-c to generate object file from assembly fileunderstandable by machine
-gcc file.o -o file
gprof
Enable profiling while compilation by -pg option
$gcc -pg -o add_main add_main.c
//This will generate add_main executable with profiling enabled
$./add_main
//This will execute the binary with generating profiling output
file gmon.out
$gprof add_main gmon.out
//gprof is run with executable and profiling output file which
combined produces analysis information
Code Navigation
To find function definitions- ctags
To find function calls of particular hierarchycscope
Project Making
Requirement Analysis
Top Level Design
Detail Design
Coding
Creation of library and using it in your program
Deletion of a module from library
Project creation/building: using make, cmake
Valgrind
$sudo apt-get install valgrind
$valgrind tool=TOOLNAME ./EXECUTABLENAME
TOOLNAME can be memcheck, callgrind, cachegrind, helgrind,
massif, DRD etc...
For more options you can read $man valgrind
memcheck
This tool can detect the following memory related problems :
Use of uninitialized memory
Reading/writing memory after it has been freed
Reading/writing off the end of mallocd blocks
Memory leaks
Mismatched use of malloc/new/new[] vs free/delete/delete[]
Doubly freed memory
Memory leaks
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p = malloc(1);
*p = 'a';
char c = *p;
printf("\n [%c]\n",c);
return 0;
}
#include <stdio.h>
Mismatched use of
malloc/new/new[] vs
free/delete/delete[]
#include <stdlib.h>
#include<iostream>
int main(void)
{
char *p = (char*)malloc(1);
*p = 'a';
char c = *p;
printf("\n [%c]\n",c);
delete p;
return 0;
}
Cachegrind
To make your executable faster
$valgrind tool=cachegrind ./a.out
$ cg_annotate --show=Dr,Dw
cachegrind.out.<processID of your code> | grep -v
"???"
Cachegrind shows how many times your program
accessed memory to read or write data
The portion or function having more memory access
can be optimized for the same
Callgrind
$valgrind tool=callgrind ./a.out
$callgrind_annotate callgrind.out.<ProcessID
of code>
This will tell you which functions used the most
instructions to run
the functions having maximum instruction can be
modified in such a way that takes minimum
instructions