Instalar herramientas básicas en Debian
# aptitude install g++ valgrind valgrind-mpi kcachegrind cppcheck astyle
Compilar fuentes en C++ solicitando que el ejecutable tenga el nombre EJEMP
$ g++ -O1 *.cpp -o ejemp
para ejecutar el programa ya compilado:
$ ./ejemp
Compilar usando diversas optimizaciones (-O1, -O2 o -O3) usar:
$ g++ -O1 *.cpp
para ejecutar el programa ya compilado:
$ ./a.out
Para compilar y ver todos los avisos usar:
$ g++ -pedantic -Wall -Wextra -O *.cpp
o
$ g++ -Weffc++ *.cpp
Para hacer análisis de rendimiento, entonces
$ g++ -g -pg -O0 *.cpp
$ ./a.out
$ gprof -c -z a.out gmon.out > sal.txt
el archivo sal.txt contiene el análisis de rendimiento detallado.
Para hacer depuración del código mediante el depurador gráfico ddd usar:
$ g++ -g -O0 *.cpp
$ ddd ./a.out
Puede usarse también los depuradores xxgdb, gdb, kdbg cada uno tiene sus
pros y contras, depende del usuario cual es el más adecuado para usar
Para el rastreo de problemas con la manipulación de memoria y punteros desbordados:
$ g++ -g -O0 *.cpp
$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out
o analizar la salida usando kcachegrind
$ valgrind --tool=callgrind ./a.out
$ kcachegrind profile.callgrind
Existen varios front-end para valgrind, uno de ellos es alleyoop y se usa:
$ alleyoop ./a.out -v --arg1=foo
otro es kcachegrind, más opciones en:
http://valgrind.org/
http://alleyoop.sourceforge.net/usage.html
http://kcachegrind.sourceforge.net/html/Home.html
Revisión estática del código en C++ usar
$ cppcheck --enable=all *.?pp
mostrara los avisos de análisis estático del código indicado
Formateador automático de código fuente para C, C++, C#
$ astyle -s3 -p --style=allman --lineend=linux *.?pp
para Java, una opción es
$ astyle -s3 -p --style=java --lineend=linux *.java
más opciones en:
http://astyle.sourceforge.net/astyle.html
Herramientas para convertir fuentes en HTML
$ code2html Fuente Salida.html
$ java2html Fuentes
$ c2html Fuente
Convertir páginas de un PDF a JPG
# apt-get install imagemagick
$ convert -density 800 Archivo.pdf Archivo.jpg
Concatenar varios PDFs en uno solo
# apt-get install pdftk
$ pdftk archivo-1.pdf archivo-2.pdf [archivo-n.pdf] cat output archivo-final.pdf
Convertir fuente en PDF
$ nl test.cpp | a2ps -1 -l100 -otest.ps ; ps2pdf test.ps
^ ^ ^
| | |___ convierte .PS a .PDF
| |_______ Genera del fuente numerado un .PS
|
|________ Numera las líneas del fuente
Si se tiene que ejecutar múltiples programas que son independientes uno de
otro se puede usar parallel para correr N de ellos al mismo tiempo, por
ejemplo si tenemos un archivo: hacer.bash con el contenido
./a.out 4 5 4 > a1.txt
./a.out 4 5 3 > a2.txt
./a.out 4 5 6 > a3.txt
./a.out 4 5 4 > a4.txt
entonces podemos ejecutarlo usando parallel, el programa usara el numero
máximo de cores disponibles:
$ parallel -v < hacer.bash
si solo se desea usar una determinada cantidad de cores (por ejemplo 3)
entonces usamos:
$ parallel -v -j 2 < hacer.bash
Otras herramientas:
DUMA
DUMA helps you detect two common programming bugs:
software that overruns the boundaries of a malloc() memory allocation, and
software that touches a memory allocation that has been released by free().
Unlike other malloc() debuggers, DUMA will detect read accesses as well as
writes, and it will pinpoint the exact instruction that causes an error.
It has been in use at Pixar since 1987, and at many other sites for years.
GCC
option -Warray-bounds up from gcc 4.3
options -fmudflap -fmudflapth -fmudflapir up from gcc 4.0
See http://gcc.gnu.org/
See http://gcc.gnu.org/wiki/Mudflap_Pointer_Debugging
IBM Stack Smashing Protector aka Pro Police
it is a GCC (Gnu Compiler Collection) extension for protecting applications
from stack-smashing attacks. Applications written in C will be protected by
the method that automatically inserts protection code into an application at
compilation time. The protection is realized by buffer overflow detection and
the variable reordering feature to avoid the corruption of pointers. The basic
idea of buffer overflow detection comes from StackGuard system.
See http://www.trl.ibm.com/projects/security/ssp/
Checkergcc
a modified version of the GNU C Compiler that instruments all memory
references, is available on Linux systems and where GCC is used. It performs
some of the same tasks as Purify, but only on code that it has compiled.
Valgrind
Valgrind is an award-winning suite of tools for debugging and profiling Linux
programs. With the tools that come with Valgrind, you can automatically detect
many memory management and threading bugs, avoiding hours of frustrating
bug-hunting, making your programs more stable. You can also perform detailed
profiling, to speed up and reduce memory use of your programs.
The Valgrind distribution currently includes four tools: a memory error
detector, a cache (time) profiler, a call-graph profiler, and a heap (space)
profiler. It runs on the following platforms: X86/Linux, AMD64/Linux,
PPC32/Linux, PPC64/Linux.
See http://valgrind.org/
Pageheap.exe
Another alternative to using duma on Windows (XP, 2000, Server 2003) is to use
the built in heap debugging provided by Microsoft. It's not as feature rich as
duma, but for complex projects were duma is causing issues it will work with less
hassle (it sits in the heap manager itself).
You can control it using the global flags (gflags) utility that comes with windbg.
You can enable it by saying: gflags.exe /p /full /enable MyProgram.exe
And do unaligned by saying: gflags.exe /p /full /unaligned /enable MyProgram.exe
gflags will set specific registry keys to enable the pageheap debugging on the
executable. You can disable it using the "/disable" flag.
See http://support.microsoft.com/kb/286470
MPATROL
The mpatrol library is a powerful debugging tool that attempts to diagnose
run-time errors that are caused by the wrong use of dynamically allocated
memory. It acts as a malloc() debugger for debugging dynamic memory
allocations, although it can also trace and profile calls to malloc()
and free() too.
See http://www.cbmamiga.demon.co.uk/mpatrol/
Purify
does a much more thorough job than DUMA, and does not have the huge memory
overhead.
LibSafe
protects Critical Elements of Stack.
See http://www.research.avayalabs.com/
DieHard
helps buggy programs run correctly and protects them from a range of security
vulnerabilities.
See http://www.diehard-software.org/
Gperftools
Gperftools from Google provides a set of tools aimed for analyzing and improving performance
of multi-threaded applications. They offer a CPU profiler, a fast thread aware malloc
implementation, a memory leak detector and a heap profiler. We focus on their sampling
based CPU profiler.
Creating a CPU profile of selected parts of your application with gperftools requires the
following steps:
compile your program with debugging symbols enabled (to get a meaningful call graph)
and link gperftools profiler.so
#include and surround the sections you want to profile with
ProfilerStart("nameOfProfile.log"); and ProfilerStop();
execute your program to generate the profiling data file(s)
To analyze the profiling data, use pprof (distributed with gperftools) or convert it to
a callgrind compatible format and analyze it with KCachegrind
Let's apply this our demo application (profile_gperftools.sh):
#!/bin/bash
# build the program; For our demo program, we specify -DWITHGPERFTOOLS to enable the gperftools specific #ifdefs
g++ -std=c++11 -DWITHGPERFTOOLS -lprofiler -g ../cpuload.cpp -o cpuload
# run the program; generates the profiling data file (profile.log in our example)
./cpuload
# convert profile.log to callgrind compatible format
pprof --callgrind ./cpuload profile.log > profile.callgrind
# open profile.callgrind with kcachegrind
kcachegrind profile.callgrind
Alternatively, profiling the whole application can be done without any changes or
recompilation/linking, but I will not cover this here as this is not the recommended
approach. But you can find more about this in the docs.
The gperftools profiler can profile multi-threaded applications. The run time overhead
while profiling is very low and the applications run at "native speed". We can again use
KCachegrind for analyzing the profiling data after converting it to a cachegrind compatible
format. I also like the possibility to be able to selectively profile just certain areas of the code,
and if you want to, you can easily extend your program to enable/disable profiling at runtime.
-------------------------------------------------------------------------------------------------------
Algo de linux: http://www.mmc.geofisica.unam.mx/acl/linux/
Antonio Carrillo Ledesma
http://www.mmc.geofisica.unam.mx/acl/