mirror of
https://github.com/tobast/libunwind-eh_elf.git
synced 2025-01-08 18:33:42 +01:00
eh_elf: parse and store memory map
This commit is contained in:
parent
a77b0cd7bd
commit
8b7b8659ec
2 changed files with 150 additions and 0 deletions
102
src/eh_elf/memory_map.c
Normal file
102
src/eh_elf/memory_map.c
Normal file
|
@ -0,0 +1,102 @@
|
|||
#include "memory_map.h"
|
||||
#include <libgen.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
static mmap_entry_t* _memory_map = NULL;
|
||||
static size_t _memory_map_size = 0;
|
||||
|
||||
/// Init the memory map with a given /proc/XX/ argument
|
||||
int mmap_init_procdir(const char* procdir);
|
||||
|
||||
|
||||
int mmap_init_local() {
|
||||
return mmap_init_procdir("/proc/self/");
|
||||
}
|
||||
|
||||
|
||||
int mmap_init_pid(pid_t pid) {
|
||||
char procdir[64];
|
||||
sprintf(procdir, "/proc/%d/", pid);
|
||||
return mmap_init_procdir(procdir);
|
||||
}
|
||||
|
||||
int mmap_init_procdir(const char* procdir) {
|
||||
// This function reads /proc/pid/maps and deduces the memory map
|
||||
|
||||
// Open the mmap file
|
||||
char map_path[128];
|
||||
sprintf(map_path, "%s/maps", procdir);
|
||||
FILE* map_handle = fopen(map_path, "r");
|
||||
if(map_handle == NULL)
|
||||
return -1;
|
||||
|
||||
// Get line count
|
||||
int nb_entries = 0;
|
||||
int lastch;
|
||||
while((lastch = fgetc(map_handle)) != EOF) {
|
||||
if(lastch == '\n')
|
||||
nb_entries++;
|
||||
}
|
||||
rewind(map_handle);
|
||||
_memory_map = (mmap_entry_t*) calloc(nb_entries, sizeof(mmap_entry_t));
|
||||
_memory_map_size = nb_entries;
|
||||
|
||||
// Read all lines
|
||||
uintptr_t ip_beg, ip_end, offset, inode;
|
||||
char is_x;
|
||||
char path[256];
|
||||
int cur_entry = 0;
|
||||
while(fscanf(map_handle,
|
||||
"%lX-%lX %*c%*c%c%*c %lX %*[0-9a-fA-F:] %ld %s",
|
||||
&ip_beg, &ip_end, &is_x, &offset, &inode, path) != EOF)
|
||||
{
|
||||
if(cur_entry >= nb_entries) {
|
||||
mmap_clear();
|
||||
return -2; // Bad entry count, somehow
|
||||
}
|
||||
|
||||
if(inode == 0) // Special region, out of our scope
|
||||
continue;
|
||||
if(is_x != 'x') // Not executable, out of our scope
|
||||
continue;
|
||||
|
||||
_memory_map[cur_entry].id = cur_entry;
|
||||
_memory_map[cur_entry].offset = offset;
|
||||
_memory_map[cur_entry].beg_ip = ip_beg;
|
||||
_memory_map[cur_entry].end_ip = ip_end;
|
||||
_memory_map[cur_entry].object_name =
|
||||
(char*) malloc(sizeof(char) * (strlen(path) + 1));
|
||||
strcpy(_memory_map[cur_entry].object_name, path);
|
||||
|
||||
cur_entry++;
|
||||
}
|
||||
|
||||
// Shrink _memory_map to only use up the number of relevant entries
|
||||
assert(_memory_map_size >= cur_entry);
|
||||
_memory_map_size = cur_entry; // Because of skipped entries
|
||||
_memory_map = (mmap_entry_t*)
|
||||
realloc(_memory_map, _memory_map_size * sizeof(mmap_entry_t));
|
||||
|
||||
// dlopen corresponding eh_elf objects
|
||||
for(size_t id = 0; id < _memory_map_size; ++id) {
|
||||
char eh_elf_path[256];
|
||||
char* obj_basename = basename(_memory_map[id].object_name);
|
||||
sprintf(eh_elf_path, "%s.eh_elf.so", obj_basename);
|
||||
_memory_map[id].eh_elf = dlopen(eh_elf_path, RTLD_LAZY);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mmap_clear() {
|
||||
if(_memory_map != NULL) {
|
||||
for(size_t pos=0; pos < _memory_map_size; ++pos) {
|
||||
free(_memory_map[pos].object_name);
|
||||
dlclose(_memory_map[pos].eh_elf);
|
||||
}
|
||||
free(_memory_map);
|
||||
}
|
||||
}
|
48
src/eh_elf/memory_map.h
Normal file
48
src/eh_elf/memory_map.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
/********** Libunwind -- eh_elf flavour **********
|
||||
* This is the eh_elf version of libunwind, made for academic purposes.
|
||||
*
|
||||
* Théophile Bastian <theophile.bastian@ens.fr> <contact+github@tobast.fr>
|
||||
*************************************************
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdint.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
/// A type representing a dlopen handle
|
||||
typedef void* dl_obj_t;
|
||||
|
||||
/// A structure containing the informations gathererd about a line in the
|
||||
/// memory map
|
||||
typedef struct {
|
||||
int id; ///< ID of this entry, for fast access
|
||||
uintptr_t offset; ///< Total offset: ip + offset = ip in original ELF file
|
||||
char* object_name; ///< Name of the object mapped here
|
||||
uintptr_t beg_ip, end_ip; ///< Start and end IP of this object in memory
|
||||
dl_obj_t eh_elf; ///< Corresponding eh_elf file, dlopen'd
|
||||
} mmap_entry_t;
|
||||
|
||||
/// Dealloc all allocated memory and reset internal state
|
||||
void mmap_clear();
|
||||
|
||||
/** Init the memory map for the local process
|
||||
* @returns 0 upon success, or a negative value upon failure.
|
||||
**/
|
||||
int mmap_init_local();
|
||||
|
||||
/** Init the memory map for a remote process with the given pid
|
||||
* @returns 0 upon success, or a negative value upon failure.
|
||||
**/
|
||||
int mmap_init_pid(pid_t pid);
|
||||
|
||||
/// Get the `mmap_entry_t` corresponding to the given IP
|
||||
int mmap_get_entry(uintptr_t ip, mmap_entry_t* entry);
|
Loading…
Reference in a new issue