Dump Memory for Post-Processing
Learn to dump memory for later analysis, retrieve function and variable lists, load symbols, and inspect variables.
In this lesson, we’ll see how we can collect information like memory contents, lists of functions, and module variables for post-processing. We’ll also see how we can load symbols and inspect arguments and local variables.
Application source code
We have designed a multi-threaded application for this task.
Press + to interact
// Build:// g++ main.cpp -g -pthread -static -o App12#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <string.h>#include <stdlib.h>pthread_mutex_t mutexA, mutexB;void procC(){throw 0;}void procA(){try{pthread_mutex_lock(&mutexA);procC();pthread_mutex_unlock(&mutexA);}catch (...){}sleep(20);pthread_mutex_lock(&mutexB);pthread_mutex_unlock(&mutexB);}void procB(){pthread_mutex_lock(&mutexB);pthread_mutex_lock(&mutexA);sleep(30);pthread_mutex_lock(&mutexA);pthread_mutex_lock(&mutexB);}#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[]){pthread_mutex_init(&mutexA, NULL);pthread_mutex_init(&mutexB, NULL);THREAD_CREATE(one)THREAD_CREATE(two)sleep(10);THREAD_CREATE(three)THREAD_CREATE(four)THREAD_CREATE(five)sleep(-1);return 0;}
Loading the core dump
We can load up the core dump file using the following command:
gdb -c App12.core.698 -se App12
The above command will output the following to the terminal:
...