Detect NULL Pointer Exceptions Due to Data
Gain knowledge on identifying NULL pointer exceptions caused by data errors and discovering problematic threads and instructions.
Application source code
We have created a multi-threaded application to determine if a NULL
pointer error in our data is causing the segmentation fault.
Note: It’s alright if we are unable to identify the cause of the fault.
// Build:// gcc main.c -pthread -static -o App2D#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <string.h>#include <stdlib.h>void procA(){int *p = NULL;*p = 1;}void procB(){sleep(1);void (*pf)() = NULL;pf();}#define THREAD_DECLARE(num,func) void bar_##num() \{ \func; \} \\void foo_##num() \{ \bar_##num(); \} \\void * thread_##num (void *arg) \{ \foo_##num(); \\return 0; \}THREAD_DECLARE(one,sleep(-1))THREAD_DECLARE(two,procA())THREAD_DECLARE(three,sleep(-1))THREAD_DECLARE(four,procB())THREAD_DECLARE(five,sleep(-1))#define THREAD_CREATE(num) {pthread_t threadID_##num; pthread_create (&threadID_##num, NULL, thread_##num, NULL);}int main(int argc, const char * argv[]){THREAD_CREATE(one)THREAD_CREATE(two)THREAD_CREATE(three)THREAD_CREATE(four)THREAD_CREATE(five)sleep(3);return 0;}
Loading the core dump
The first thing we are going to do is load the dump file that we have included with the course.
gdb -c core.App2D -se App2D
As discussed in the “Fundamentals of Core Dump Analysis,” this command:
Loads the core dump
core.App2D
file.Loads the symbol table from the
App2D
application binary.Treats
App2D
as containing the code that is executed during debugging, whenever needed.
Listing all threads
From this output, we notice that there was a segmentation fault that terminated the program. The next step in our investigation should be to list all threads of our program to identify which thread caused the segmentation fault. We list ...