Identify Active Threads
Learn how to identify active threads.
We talked about multi-threaded applications throughout the course. In this lesson, we’ll learn how to identify active threads. We can take a look at all of the threads present in our application to find what’s causing this.
Application source code
We have created a multi-threaded application that encounters a divide-by-zero error and runs into a floating-point exception:
Press + to interact
// Build:// gcc main.c -pthread -static -o App7#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <string.h>#include <stdlib.h>void procF(int i){int buffer[1024] = {-1, 0, i + 1, 0, -1};procF(buffer[2]);}void procE(){procF(1);}int procD(int a, int b){return a / b;}int procC(){return procD(1, 0);}void procB(char *buffer){char data[100] = "My New Bigger Buffer";memcpy(buffer, data, sizeof(data));}void procA(){char data[10] = "My Buffer";procB(data);}#define THREAD_DECLARE(num, func) void bar_##num() \{ \sleep(10); \func; \} \\void foo_##num() \{ \bar_##num(); \} \\void * thread_##num (void *arg) \{ \foo_##num(); \\return 0; \}THREAD_DECLARE(one, procA())THREAD_DECLARE(two, sleep(-1))THREAD_DECLARE(three, procC())THREAD_DECLARE(four, sleep(-1))THREAD_DECLARE(five, procE())#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(-1);return 0;}
Loading the core dump
We will load the core dump file using the following command:
gdb -c core.App7 -se App7
The above command will output the following ...
Access this course and 1400+ top-rated courses and projects.