Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BT_BUF_SIZE 100
#include <link.h>
#include <stdio.h>
#include <stdlib.h>
static int
callback(struct dl_phdr_info *info, size_t size, void *data)
{
int j;
static int once = 0;
if (once) return 0;
once = 1;
printf("relocation: 0x%lx\n", (long)info->dlpi_addr);
for (j = 0; j < info->dlpi_phnum; j++) {
if (info->dlpi_phdr[j].p_type == PT_LOAD) {
printf("a.out loaded at %p %p\n",
(void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr), (long)info->dlpi_phdr[j].p_memsz);
//break;
}
}
return 0;
}
int
main(int argc, char *argv[])
{
int j, nptrs;
void *buffer[BT_BUF_SIZE];
char **strings;
nptrs = backtrace(buffer, BT_BUF_SIZE);
printf("backtrace() returned %d addresses\n", nptrs);
for (int i = 0; i < nptrs; i++) {
printf("[%p ]\n", buffer[i]);
}
dl_iterate_phdr(callback, NULL);
exit(EXIT_SUCCESS);
}
nad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment