Notes By -
Day 2 : Advanced Programming
Ritik Fulzele
( CDAC Pune)
bt ==> back trace
b => break
r => run
it adds the dwarf section ,
Added
dwarf section ELF Section
/* debug_me.c – A playground for learning GDB */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#define ARRAY_SIZE 10
/* Global variables – easy to watch */
int global_counter = 0;
char *global_ptr = NULL;
/* Deliberate segfault handler */
void segfault_handler(int sig) {
printf("Caught signal %d – continuing...\n", sig);
/* Do nothing – let GDB catch it */
}
/* Function with many bugs */
void buggy_function(int *arr, int n) {
int i;
char local_buffer[8];
/* 1. Buffer overflow */
strcpy(local_buffer, "THIS IS TOO LONG");
/* 2. Use-after-free */
char *tmp = malloc(20);
strcpy(tmp, "temporary");
free(tmp);
printf("Use-after-free: %s\n", tmp); /* BUG */
/* 3. Null pointer dereference */
if (global_ptr) {
printf("Global ptr: %s\n", global_ptr);
} else {
printf("Global ptr is NULL – dereferencing anyway...\n");
*global_ptr = 'X'; /* BUG */
}
/* 4. Out-of-bounds array access */
for (i = 0; i <= n; i++) { /* BUG: i <= n */
arr[i] = i * 10;
}
/* 5. Infinite loop (break with Ctrl-C) */
printf("Entering infinite loop... (send SIGINT)\n");
while (1) {
global_counter++;
usleep(100000); /* 0.1 sec */
}
}
/* Thread function */
void *thread_func(void *arg) {
int id = *(int*)arg;
printf("Thread %d started\n", id);
while (1) {
printf("Thread %d: counter = %d\n", id, global_counter);
sleep(1);
}
return NULL;
}
int main(int argc, char *argv[]) {
int local_array[ARRAY_SIZE] = {0};
pthread_t t1, t2;
int id1 = 1, id2 = 2;
/* Install signal handler */
signal(SIGSEGV, segfault_handler);
signal(SIGINT, SIG_IGN); /* Ignore Ctrl-C in program, let GDB handle */
printf("=== GDB Learning Program ===\n");
printf("Compile with: gcc -g -pthread -o debug_me debug_me.c\n");
printf("Run with: gdb ./debug_me\n\n");
/* 6. Division by zero */
int divisor = 0;
if (argc > 1) divisor = atoi(argv[1]);
printf("Division result: %d\n", 100 / divisor); /* BUG if argc==1 */
/* 7. Call buggy function */
buggy_function(local_array, ARRAY_SIZE - 1);
/* 8. Spawn threads */
pthread_create(&t1, NULL, thread_func, &id1);
pthread_create(&t2, NULL, thread_func, &id2);
pthread_join(t1, NULL);
return 0;
}
Compile the code =======> gcc -g -pthread -o debug_me debug_me.c
Run the code ====> gdb ./debug_me
ptrace() system call ==>
trace system call ==>
a) ptrace system call
b) ltrace system call
c) ftrace
Multi-threaded Dubbuging
cudaGDB
rockGDB
list command ====> It will show the whole source code when i am in a GDB Mode
sinfo
srun --partition=cpu --node=1 --ntasks-per-node=20 --time=00:20:00 --pty bash
main is located at this addr
see the list of all breakpoints
breakpoint is set
see the local variable in the code
It will jump to next breakpoint
it will print the value of j , which is mentioned in the code
press "enter"
run
see the thread information in the program
delete the break point 2
it will show the register
values
it will show the assembly code
of the given code which we actually run