Você está na página 1de 18

Some important tools and utilities

Prepared by
Shreeji Sheth

gcc

Profiling: gprof,valgrind

gdb

Code nevigation: Ctags and Cscope

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

Dynamic memory allocation


Malloc
Calloc
Memory leak and dangling pointer
bigo notation//after data structures

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

C and Assembly interaction


Inline assembly language
Linking of two assembly files
Memory models
C and Segments in Library
Linking assembly Procedure in C program

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

Use of uninitialized memory


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p;
char c = *p;
printf("\n [%c]\n",c);
return 0;
}

Reading/writing memory after it has


been freed
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p = malloc(1);
*p = 'a';
char c = *p;
printf("\n [%c]\n",c);
free(p);
c = *p;
return 0;
}

Reading/writing off the end of


mallocd blocks
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p = malloc(1);
*p = 'a';
char c = *(p+1);
printf("\n [%c]\n",c);
free(p);
return 0;
}

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;
}

Doubly freed memory


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p = (char*)malloc(1);
*p = 'a';
char c = *p;
printf("\n [%c]\n",c);
free(p);
free(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

Você também pode gostar