SlideShare a Scribd company logo
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
1/10
Debugging Tools
Matteo Virgilio
Politecnico di Torino
matteo.virgilio@polito.it
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
2/10
Valgrind
• Valgrind is an open source tool for memory debugging and
it is available for Linux.
• Can be exploited to:
 Find memory leaks;
 Find Invalid Pointer Use
 Detect The Use Of Uninitialized Variables
 Etc etc…
• Complete documentation available at:
http://valgrind.org/docs/
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
3/10
Valgrind brief HOWTO
1. Compile your code with –g option in GCC.
2. Run your program within the Valgrind environment.
Assuming your program is executed with the following cmd
line:
./server 1500
You can simply invoke:
valgrind ./server 1500
3. After quitting your program (e.g. Ctrl-C), you will get the
Valgrind output.
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
4/10
Example: a simple calculator
#include <stdio.h>
#include <stdlib.h>
#define N 1024
int main(int argc, char* argv[])
{
char* buffer;
int op1, op2;
while(1)
{
buffer = (char*)malloc(N*sizeof(char));
printf ("Insert two integers: ");
fgets (buffer, N, stdin);
sscanf (buffer, "%d %d", &op1, &op2);
printf ("Result=%dn", op1+op2);
}
}
gcc –g –o calc calc.c
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
5/10
Output with Valgrind: memory leak found!
mettiu@mettiu-virtual-machine:~/valgrind_example$ valgrind ./calc
==5857== Memcheck, a memory error detector
==5857== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==5857== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==5857== Command: ./calc
==5857==
Insert two integers: 10 10
Result=20
Insert two integers: ^C==5857==
==5857== HEAP SUMMARY:
==5857== in use at exit: 2,048 bytes in 2 blocks
==5857== total heap usage: 2 allocs, 0 frees, 2,048 bytes allocated
==5857==
==5857== LEAK SUMMARY:
==5857== definitely lost: 1,024 bytes in 1 blocks
==5857== indirectly lost: 0 bytes in 0 blocks
==5857== possibly lost: 0 bytes in 0 blocks
==5857== still reachable: 1,024 bytes in 1 blocks
==5857== suppressed: 0 bytes in 0 blocks
==5857== Rerun with --leak-check=full to see details of leaked memory
==5857==
==5857== For counts of detected and suppressed errors, rerun with: -v
==5857== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
6/10
The revised code
#include <stdio.h>
#include <stdlib.h>
#define N 1024
int main(int argc, char* argv[])
{
char* buffer;
int op1, op2;
while(1)
{
buffer = (char*)malloc(N*sizeof(char));
printf ("Insert two integers: ");
fgets (buffer, N, stdin);
sscanf (buffer, "%d %d", &op1, &op2);
printf ("Result=%dn", op1+op2);
free (buffer);// Release the allocated memory to the OS
}
}
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
7/10
Output with Valgrind: everything ok!
mettiu@mettiu-virtual-machine:~/valgrind_example$ valgrind ./calc
==5884== Memcheck, a memory error detector
==5884== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==5884== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==5884== Command: ./calc
==5884==
Insert two integers: 10 10
Result=20
Insert two integers: ^C==5884==
==5884== HEAP SUMMARY:
==5884== in use at exit: 1,024 bytes in 1 blocks
==5884== total heap usage: 2 allocs, 1 frees, 2,048 bytes allocated
==5884==
==5884== LEAK SUMMARY:
==5884== definitely lost: 0 bytes in 0 blocks
==5884== indirectly lost: 0 bytes in 0 blocks
==5884== possibly lost: 0 bytes in 0 blocks
==5884== still reachable: 1,024 bytes in 1 blocks
==5884== suppressed: 0 bytes in 0 blocks
==5884== Rerun with --leak-check=full to see details of leaked memory
==5884==
==5884== For counts of detected and suppressed errors, rerun with: -v
==5884== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
8/10
Wireshark
• Wireshark is one of the most famous packet analyzers.
• Cross-platform
• Simple interaction thanks to the advanced GUI provided
• Use wireshark to look at the actual data sent to/by your
applications! 
• http://www.wireshark.org/
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
9/10
Wireshark brief HOWTO
1. Start wireshark (may require root privileges)
2. Select the correct interface on which to capture (loopback
if you are working on your local machine)
3. Start the capture
4. Run your data transfer/program to debug
5. Stop the capture
6. Analyze wireshark output
Matteo Virgilio, Politecnico di Torino – 5° May, 2014
10/10
An example
Packets list
Packet information
Raw packet representation

More Related Content

PDF
Pre New Year Check of PostgreSQL
PDF
Valgrind
PDF
Silicon Valley JUG: JVM Mechanics
DOCX
Reporte de electrónica digital con VHDL: practica 7 memorias
PDF
Zone IDA Proc
PDF
ESIL - Universal IL (Intermediate Language) for Radare2
PDF
Better Embedded 2013 - Detecting Memory Leaks with Valgrind
Pre New Year Check of PostgreSQL
Valgrind
Silicon Valley JUG: JVM Mechanics
Reporte de electrónica digital con VHDL: practica 7 memorias
Zone IDA Proc
ESIL - Universal IL (Intermediate Language) for Radare2
Better Embedded 2013 - Detecting Memory Leaks with Valgrind

Similar to Debugging tools (20)

PDF
Valgrind tutorial
PDF
How to Perform Memory Leak Test Using Valgrind
PDF
Davide Berardi - Linux hardening and security measures against Memory corruption
DOCX
Valgrind debugger Tutorial
DOCX
SDN Training - Open daylight installation + example with mininet
PDF
Errors detected in C++Builder
PDF
Checking Notepad++: five years later
PPTX
Valgrind tool
PDF
Varnish in action phpuk11
PDF
Hardwear.io 2018 BLE Security Essentials workshop
PDF
Scala to assembly
PPTX
Not So Common Memory Leaks in Java Webinar
PDF
Student exercise guide_training_cmode_8.2
PDF
Varnish in action confoo11
PDF
Checking the Cross-Platform Framework Cocos2d-x
PDF
ZooKeeper Recipes and Solutions
PDF
ZooKeeper Recipes and Solutions
PDF
ZooKeeper Recipes and Solutions
PDF
Analyzing the Blender project with PVS-Studio
PDF
Memory Leak Debuging in the Semi conductor Hardwares
Valgrind tutorial
How to Perform Memory Leak Test Using Valgrind
Davide Berardi - Linux hardening and security measures against Memory corruption
Valgrind debugger Tutorial
SDN Training - Open daylight installation + example with mininet
Errors detected in C++Builder
Checking Notepad++: five years later
Valgrind tool
Varnish in action phpuk11
Hardwear.io 2018 BLE Security Essentials workshop
Scala to assembly
Not So Common Memory Leaks in Java Webinar
Student exercise guide_training_cmode_8.2
Varnish in action confoo11
Checking the Cross-Platform Framework Cocos2d-x
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and Solutions
Analyzing the Blender project with PVS-Studio
Memory Leak Debuging in the Semi conductor Hardwares
Ad

Recently uploaded (20)

PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
history of c programming in notes for students .pptx
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Website Design Services for Small Businesses.pdf
PDF
AutoCAD Professional Crack 2025 With License Key
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
iTop VPN Crack Latest Version Full Key 2025
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Design an Analysis of Algorithms II-SECS-1021-03
17 Powerful Integrations Your Next-Gen MLM Software Needs
Reimagine Home Health with the Power of Agentic AI​
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Designing Intelligence for the Shop Floor.pdf
Design an Analysis of Algorithms I-SECS-1021-03
history of c programming in notes for students .pptx
Weekly report ppt - harsh dattuprasad patel.pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Website Design Services for Small Businesses.pdf
AutoCAD Professional Crack 2025 With License Key
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
Operating system designcfffgfgggggggvggggggggg
iTop VPN Crack Latest Version Full Key 2025
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Ad

Debugging tools

  • 1. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 1/10 Debugging Tools Matteo Virgilio Politecnico di Torino matteo.virgilio@polito.it
  • 2. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 2/10 Valgrind • Valgrind is an open source tool for memory debugging and it is available for Linux. • Can be exploited to:  Find memory leaks;  Find Invalid Pointer Use  Detect The Use Of Uninitialized Variables  Etc etc… • Complete documentation available at: http://valgrind.org/docs/
  • 3. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 3/10 Valgrind brief HOWTO 1. Compile your code with –g option in GCC. 2. Run your program within the Valgrind environment. Assuming your program is executed with the following cmd line: ./server 1500 You can simply invoke: valgrind ./server 1500 3. After quitting your program (e.g. Ctrl-C), you will get the Valgrind output.
  • 4. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 4/10 Example: a simple calculator #include <stdio.h> #include <stdlib.h> #define N 1024 int main(int argc, char* argv[]) { char* buffer; int op1, op2; while(1) { buffer = (char*)malloc(N*sizeof(char)); printf ("Insert two integers: "); fgets (buffer, N, stdin); sscanf (buffer, "%d %d", &op1, &op2); printf ("Result=%dn", op1+op2); } } gcc –g –o calc calc.c
  • 5. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 5/10 Output with Valgrind: memory leak found! mettiu@mettiu-virtual-machine:~/valgrind_example$ valgrind ./calc ==5857== Memcheck, a memory error detector ==5857== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==5857== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==5857== Command: ./calc ==5857== Insert two integers: 10 10 Result=20 Insert two integers: ^C==5857== ==5857== HEAP SUMMARY: ==5857== in use at exit: 2,048 bytes in 2 blocks ==5857== total heap usage: 2 allocs, 0 frees, 2,048 bytes allocated ==5857== ==5857== LEAK SUMMARY: ==5857== definitely lost: 1,024 bytes in 1 blocks ==5857== indirectly lost: 0 bytes in 0 blocks ==5857== possibly lost: 0 bytes in 0 blocks ==5857== still reachable: 1,024 bytes in 1 blocks ==5857== suppressed: 0 bytes in 0 blocks ==5857== Rerun with --leak-check=full to see details of leaked memory ==5857== ==5857== For counts of detected and suppressed errors, rerun with: -v ==5857== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
  • 6. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 6/10 The revised code #include <stdio.h> #include <stdlib.h> #define N 1024 int main(int argc, char* argv[]) { char* buffer; int op1, op2; while(1) { buffer = (char*)malloc(N*sizeof(char)); printf ("Insert two integers: "); fgets (buffer, N, stdin); sscanf (buffer, "%d %d", &op1, &op2); printf ("Result=%dn", op1+op2); free (buffer);// Release the allocated memory to the OS } }
  • 7. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 7/10 Output with Valgrind: everything ok! mettiu@mettiu-virtual-machine:~/valgrind_example$ valgrind ./calc ==5884== Memcheck, a memory error detector ==5884== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==5884== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==5884== Command: ./calc ==5884== Insert two integers: 10 10 Result=20 Insert two integers: ^C==5884== ==5884== HEAP SUMMARY: ==5884== in use at exit: 1,024 bytes in 1 blocks ==5884== total heap usage: 2 allocs, 1 frees, 2,048 bytes allocated ==5884== ==5884== LEAK SUMMARY: ==5884== definitely lost: 0 bytes in 0 blocks ==5884== indirectly lost: 0 bytes in 0 blocks ==5884== possibly lost: 0 bytes in 0 blocks ==5884== still reachable: 1,024 bytes in 1 blocks ==5884== suppressed: 0 bytes in 0 blocks ==5884== Rerun with --leak-check=full to see details of leaked memory ==5884== ==5884== For counts of detected and suppressed errors, rerun with: -v ==5884== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
  • 8. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 8/10 Wireshark • Wireshark is one of the most famous packet analyzers. • Cross-platform • Simple interaction thanks to the advanced GUI provided • Use wireshark to look at the actual data sent to/by your applications!  • http://www.wireshark.org/
  • 9. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 9/10 Wireshark brief HOWTO 1. Start wireshark (may require root privileges) 2. Select the correct interface on which to capture (loopback if you are working on your local machine) 3. Start the capture 4. Run your data transfer/program to debug 5. Stop the capture 6. Analyze wireshark output
  • 10. Matteo Virgilio, Politecnico di Torino – 5° May, 2014 10/10 An example Packets list Packet information Raw packet representation