This guide provides detailed information about debugging libCacheSim using the debug script.
The debug script (scripts/debug.sh) is a utility that helps you build and debug libCacheSim with GDB. It automatically:
- Sets up a debug build with proper compiler flags
- Builds the project in debug mode
- Launches GDB with thread exit messages disabled
- Passes any provided arguments to the cachesim program
# Basic usage - just build and launch GDB
./scripts/debug.sh
# Clean build and debug
./scripts/debug.sh -c
# Debug with program arguments
./scripts/debug.sh -- data/cloudPhysicsIO.vscsi vscsi lru 100m,1gb
# Clean build and debug with program arguments
./scripts/debug.sh -c -- data/cloudPhysicsIO.vscsi vscsi lru 100m,1gbThe debug script supports the following options:
-h, --help: Show help message-c, --clean: Clean the build directory before building--: Separator between script options and program arguments
./scripts/debug.shThis will:
- Create a debug build in
_build_dbgdirectory - Build the project with debug symbols
- Launch GDB with the cachesim binary
./scripts/debug.sh -cThis will:
- Remove the existing
_build_dbgdirectory - Create a fresh debug build
- Build the project with debug symbols
- Launch GDB with the cachesim binary
./scripts/debug.sh -- data/cloudPhysicsIO.vscsi vscsi lru 100m,1gbThis will:
- Build the project in debug mode
- Launch GDB with the cachesim binary
- Pass the arguments
data/cloudPhysicsIO.vscsi vscsi lru 100m,1gbto the program
To debug a specific cache algorithm, set breakpoints in the algorithm's implementation:
# Start GDB with the debug script
./scripts/debug.sh -- data/cloudPhysicsIO.vscsi vscsi lru 100m,1gb
# In GDB, set breakpoints in the LRU implementation
(gdb) b LRU_get
(gdb) b LRU_put
(gdb) rTo debug trace reading issues:
# Start GDB with a specific trace
./scripts/debug.sh -- data/cloudPhysicsIO.vscsi vscsi lru 100m,1gb
# In GDB, set breakpoints in the trace reader
(gdb) b vscsi_read_one_req
(gdb) rTo debug admission policy behavior:
# Start GDB with a specific admission policy
./scripts/debug.sh -- data/cloudPhysicsIO.vscsi vscsi lru 100m,1gb --admission adaptsize
# In GDB, set breakpoints in the admission policy
(gdb) b adaptsize_admit
(gdb) rTo debug segmentation faults:
# Start GDB with the debug script
./scripts/debug.sh -- data/cloudPhysicsIO.vscsi vscsi lru 100m,1gb
# In GDB, run the program
(gdb) r
# When the segfault occurs, examine the backtrace
(gdb) btr- run/continuec- continuen- next (step over)s- step (step into)p variable- print variableb function- set breakpointbt- backtraceq- quit
b file.c:line- set breakpoint at specific lineb function if condition- set conditional breakpointwatch variable- set watchpoint on variableinfo break- list all breakpointsdelete breakpoint_number- delete a breakpointdisable breakpoint_number- disable a breakpointenable breakpoint_number- enable a breakpointclear file.c:line- clear breakpoint at linefinish- run until current function returnsuntil line- run until line is reachedreturn value- return from current function with valuecall function(args)- call a functiondisplay variable- display variable value after each commandundisplay display_number- stop displaying a variableinfo locals- show local variablesinfo args- show function argumentsinfo threads- show all threadsthread thread_number- switch to threadframe frame_number- switch stack frameup- move up one stack framedown- move down one stack frameset print pretty on- pretty print structuresset print array on- print arrays in a more readable formatset print null-stop on- stop printing at null character in stringsset logging on- log GDB output to a fileset logging off- stop loggingshell command- execute shell commandhelp command- get help on a command
- Use
Ctrl+X Ctrl+Ato enter TUI (Text User Interface) mode for a better debugging experience - Use
Ctrl+X 2to split the screen and show source code - Use
Ctrl+X 1to return to single window mode - Use
Ctrl+Cto interrupt the program and return to GDB prompt - Use
Ctrl+Dto exit GDB - Use
Ctrl+Lto refresh the screen - Use
Ctrl+Rto reverse-search command history - Use
Ctrl+Pto search backward through command history - Use
Ctrl+Nto search forward through command history - Use
Ctrl+Oto execute the current command and fetch the next one
For memory-related issues, you can use GDB with Valgrind:
# Install Valgrind if not already installed
sudo apt install valgrind # Ubuntu/Debian
brew install valgrind # macOS
# Run cachesim with Valgrind
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --log-file=valgrind-out.txt ./_build_dbg/bin/cachesim data/cloudPhysicsIO.vscsi vscsi lru 100m,1gbIf you encounter any issues:
- Make sure you have GDB installed
- Check that all dependencies are installed
- Try cleaning the build directory with
-coption - Ensure you have proper permissions to execute the script
If GDB is not installed, install it using:
# Ubuntu/Debian
sudo apt install gdb
# CentOS/RHEL
sudo yum install gdb
# macOS
brew install gdbIf debug symbols are missing, ensure you're using the debug build:
./scripts/debug.sh -c # Clean and rebuild with debug symbolsIf GDB crashes, try:
# Start with a clean build
./scripts/debug.sh -c
# Or try with a simpler command
./scripts/debug.sh- Install the C/C++ extension for VSCode
- Create a
.vscode/launch.jsonfile with:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug libCacheSim",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/_build_dbg/bin/cachesim",
"args": ["data/cloudPhysicsIO.vscsi", "vscsi", "lru", "100m,1gb"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build-debug"
}
]
}- Create a
.vscode/tasks.jsonfile with:
{
"version": "2.0.0",
"tasks": [
{
"label": "build-debug",
"type": "shell",
"command": "bash scripts/debug.sh -c",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}- Press F5 to start debugging